/* Programmer: Greg Perkins Program: pal.c Class: CS 73 C/UNIX Due: March 10, 1998 This program will identify whether or not a string of characters (line) entered by the user is a palindrome. The program will accept a maximum of 80 characters to check. */ #include #define SIZE 81 void get_string (char *str); /* PRE: str must be declared does not have to be initialized SIZE (global variable) declared and initialized to max value desired POST: str contains user entered string of length SIZE or less */ int clean (char *str); /* PRE: str must be declared and initialized POST: str contains all alnum chars from original str (all in lowercase) fctval = number of chars in str minus '\0' */ void get_mid (const int len, int *low, int *high); /* PRE: len must represent the number of elements in an array low must be declared high must be declared POST: low holds position of center element high holds position of center element low == high, when number of elements is odd low == high + 1, when number of elements is even */ int is_palindrome (const char *str, const int len, int low, int high); /* PRE: str must contain ONLY lowercase alnum chars len must represent the number of alnum chars in str low must represent the center element of str high must represent the center element of str low == high, when number of elements is odd low == high + 1, when number of elements is even POST: fctval == 1, if no chars, or only one char, in str fctval == 1, if 2 or more chars in str are a palindrome fctval == 0, if chars in str are not a palindrome */ int main () { char line[SIZE]; int length; int low_mid, high_mid; int *l_ptr, *h_ptr; l_ptr = &low_mid; h_ptr = &high_mid; get_string(line); /* get test string from user */ printf("You entered:\n%s\n", line); /* remove all non-alnum chars and make remaining chars lowercase */ length = clean (line); /* get num of chars in line */ printf("Clean line (length = %d):\n%s\n", length, line); get_mid (length, l_ptr, h_ptr); /* find middle element(s) */ /* test line and display the results */ if (is_palindrome (line, length, low_mid, high_mid)) printf("\nYour string is a palindrome.\n"); else printf("\nYour string is NOT a palindrome.\n"); return (0); } void get_string(char *str) { char ch; int i = 0; while ((i < SIZE) && ((ch = getchar()) != EOF) && (ch != '\n')) str[i++] = ch; if (ch == '\n') str[i++] = ch; str[i] = '\0'; return; } int clean (char *str) { int i, j = 0; /* examine str[] - keep only alnum chars and convert them to lowercase */ for (i = 0; str[i] != '\0'; i++) if (isalnum(str[i])) str[j++] = tolower(str[i]); str[j] = '\0'; return (j); /* return num of chars in str not including '\0' */ } void get_mid (const int len, int *low, int *high) { *low = (len - 1) / 2; if (((len - 1) % 2) == 0) *high = *low; else *high = *low + 1; } int is_palindrome (const char *str, const int len, int low, int high) { if ((len == 0) || (len == 1)) return (1); if (str[low] == str[high]) if (low == 0) return (1); else return (is_palindrome (str, len, --low, ++high)); else return (0); }