Programs with line-by-line explanations
Prepared by: Dr. Tojo Mathew
Warning! You are warned against blindly copying the code statements. Read the explanations given for the code statements and understand the implementation. Then, try to do it yourself. Blindly re-typing the code statements is a waste of time & effort.
Table of contents
Note: Descriptions in brown font enclosed within /* and */ or starting with // are comments for human readers to understand the program better. No need to type in comments in the actual program. Compilers ignore comments while generating executable code of the program.
Whole India is tweeting about the probable names for Aishwarya Rai's daughter. Some astrologers are suggesting that she will grow up to be a more famous celebrity than Aishwarya if her name is a palindrome. As we all know, a palindrome is a word that can be read the same way in either direction.
Write a C program to determine whether a given word is a palindrome or not. Do not use any string library functions.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
Output Format:
Refer sample input and output for formatting specifications.
Sample Input and Output 1:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the word
hannah
hannah is a palindrome
Sample Input and Output 2:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the word
bina
bina is not a palindrome
Solution:
General description of program: Check the input string is a palindrome without using built-in functions for string handling.
Solution idea: Initially assume that the input string is a palindrome. Now, repeatedly compare the corresponding characters from both ends of the string, i.e, compare 1st and last character, 2nd and second last character etc. At any point, if the corresponding characters are not matching, we can conclude that our initial assumption is wrong and the input string is not palindrome. If all the corresponding characters are matching, our initial assumption is correct and the input string is a palindrome.
Whole India is tweeting about the probable names for Aishwarya Rai's daughter. Some astrologers are suggesting that she will grow up to be a more famous celebrity than Aishwarya if her name is a palindrome. As we all know, a palindrome is a word that can be read the same way in either direction.
Write a C program to determine whether a given word is a palindrome or not. Use String built-in function.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
Output Format:
Refer sample input and output for formatting specifications.
Sample Input and Output 1:
[All text in bold corresponds to the input and the rest corresponds to output.]
Enter the word
hannah
hannah is a palindrome
Sample Input and Output 2:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the word
bina
bina is not a palindrome
Solution:
General description of program: Check the input string is a palindrome using built-in functions for string handling.
Solution idea:
1. Initially assume that the input string is a palindrome. Create a copy of the input string using strcpy().
2. Reverse the copy of the string by exchanging corresponding characters from both ends repeatedly, i.e, exchange 1st and last character, 2nd and second last character etc. From both sides the exchange is done till the middle of the string to get the reversed string.
3. Compare input string with reversed string using strcmp(). If both are matching, the input string is a palindrome, otherwise not.
4. Display output appropriately.