View the step-by-step solution to:

ECE 71 - Engineering Computations in C Professor Kriehn Code Due By: Midnight on Friday, April 23 Writeup Due By: Class on Tuesday, April 27, 2010...

Write a program that prompts the user to input a string into the computer from the keyboard and calls a
function that tests whether or not the string is a palindrome.
ECE 71 – Engineering Computations in C Professor Kriehn Code Due By: Midnight on Friday, April 23 Writeup Due By: Class on Tuesday, April 27, 2010 HOMEWORK #24 – Testing a Palindrome Write a program that prompts the user to input a string into the computer from the keyboard and calls a function that tests whether or not the string is a palindrome. Print the results to the screen. Specifications: Use the following function prototype for your is_palindrome() function: bool is_palindrome(char *p); Since the bool data type is undefined on callisto, use the typedef operator to create a new data type called bool that is equivalent to a short data type. Also define two constants called TRUE and FALSE to help you use your bool data type appropriately. Define a constant called STR_SIZE that is equal to 100, representing the maximum string size you can store. The character array used to store the string should therefore contain STR_SIZE+1 elements to accommodate the NULL character at the end of the string. Since scanf() does not work well for reading in a string, and the gets() function is inherently unsafe due to the possibility of undefined behavior if you input a string greater than 100 characters, use the fgets() function instead . The fgets() function requires 3 arguments: a pointer or an address indicating memory location of the 0 th element of the array, a number that defines how many characters you are willing to read into the array, and where you are reading the string in from. See the following website for an excellent snippet of code that demonstrates how the fgets() function works: http://www.java2s.com/Code/C/Console/Usefgetstoreadstringfromstandardinput.htm In the example on the website, the character array is called str , the function is trying to read in 10 characters, and we want to read in the information from the standard input (keyboard), which is called stdin . Please also note that fgets() typically reads in the newline character as well, so most C programmers replace the ‘ \n ’ with a ‘ \0 ’. For your program, try to read in the maximum number of characters you can (if the user enters less than 100 characters, NULL characters will be placed in the other elements of the array automatically), and read the characters in from stdin . After calling your is_palindrome() function, print out “ The string is a palindrome. if function returns TRUE ; otherwise print out “ The string is not a palindrome. ”. Inside your function, define two character pointers. The first pointer should initially point to the beginning of the string, and the second pointer should initially point to the end of the string. Then check to see if the pointers are pointing to alphabetic characters, and if not, increment or decrement the two pointers appropriately. Once you are sure the pointers are pointing to alphabetic characters, check
Background image of page 1
to see if the two characters are equal to each other (ignore case). You will find the isalpha() and toupper() functions useful in this process. If the two characters are not equal to each other, return a FALSE , otherwise, advance your pointers to the next characters and repeat the process until the pointers move past each other. If you get to the point where the pointers move past each other and you have not exited the function yet, you know that you have a palindrome, in which case the function should return TRUE . If you execute the program, the following information should be displayed: ~> hw24.o Please enter a string: Too hot to HOOT! The string is a palindrome. ~> hw24.o Please enter a string: My name is Dr. Kriehn. The string is not a palindrome. ~>
Background image of page 2

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question