// reads an array length and char array and determines if it is a palindrome // CS1711 class, 1998 November 13 #include #include const int MAXPAL=50; //maximum array size for the palindrome void fill_array(char a[], int a_size, int& a_used); // fills a character array of size a_size with a_used entries // based on a function from the text int palcheck(const char a[], int asize, int alength); /* a is a character array of size asize with alength items filled return 1 if a contains a palindrome, else 0 */ void out_char_arr(char a[], int asize, int alength); /* a is a character array of size asize with alength items filled the array is sent to the output stream */ int main() { int pal_len,count = 0; char pal[MAXPAL]; cout << "\n\nWhat is the length of your palindrome?\n"; cin >> pal_len; while ((pal_len > MAXPAL)||(pal_len < 0)) //error trap { if (count > 3) {cout << "Too many bad inputs, exiting now..."; exit (1);} else {count++; cout << "Too long or short, try again...\n"; cin >> pal_len;};}; //while fill_array(pal,MAXPAL,pal_len); cout << "\n\nThe string of characters is: \n\n"; out_char_arr(pal,MAXPAL,pal_len); if (palcheck(pal,MAXPAL,pal_len)) cout << "\nThe string is a palindrome.\n"; else cout << "\nThe string is a not a palindrome"; return 0; } void fill_array(char a[], int size, int& number_used) { int index = 0; char next; cout << "Type in the appropriate number of letters\n"; do { // while index cin >> next; a[index] = next; index++; } while (index < number_used); number_used = index; } int palcheck(const char a[], int asize, int alength) /* a is a character array of size asize with alength items filled return 1 if a contains a palindrome, else 0 */ { const int TRUE = 1; const int FALSE = 0; int found, index; found = TRUE; index = 0; while (found && (index < alength/2)) { if (!(a[index] == a[alength -1 -index])) found = FALSE; else index++;} return found; } void out_char_arr(char a[], int asize, int alength) { int index=0; do { cout << a[index]; index++;} while(index < alength); cout << endl; }