C Programming Problem 1 Palindromes A palindrome is a word o
C Programming
Problem 1. Palindromes
A palindrome is a word or a phrase that reads the same backward as forward, e.g., “madam” or “nurses
run”, or “never odd or even”. For the purpose of this assignment we will ignore whitespace characters,
such as blank characters ‘ ‘, tabs ‘\\t’, comma ‘,’ and newlines ‘\ ’.
Instructions
1. Write in a file p1.c a function called isPalindrome() that takes a string as parameter and returns
TRUE (1) if the string is a palindrome or FALSE (0) otherwise. It is important to remember to ignore
(skip) all whitespace characters, so that isPalindrome(“never odd or even”) should return TRUE. The
original string without whitespaces is “neveroddoreven” and it follows the definition of a palindrome.
Write the function contract before the function.
Assume the following:
- the longest string passed as parameter has maximum 100 characters, including the ‘\\0’ at the
end.
- you should ignore whitespace and you can use function int isspace(int c) from header ctype.h
to check if a character c is whitespace.
- that strings are case sensitive – so there is no need to convert the string to the same case.
Hint: to deal with whitespace, first copy all non-whitespace characters from the string argument to a
local string variable, then check this local copy if it is a palindrome in a loop.
2. Write in file p1.c a main() function that reads from the terminal in a loop a text line using the fgets()
function and then uses the isPalindrome() function to display whether the input text is a palindrome or
not.
If the string is not a palindrome, the program displays \"not a palindrome\".
If the string is a palindrome, the program displays the palindrome and then \"is a palindrome\".
The loop should end at the end-of-file (user types CTRL-Z or CTRL-D) OR when the user enters the
word “quit”.
To read one line of text (with max. TEXTSIZE - 1 characters) declared as
char line[TEXTSIZE];
from the terminal with function fgets(), use the function call:
fgets(line, TEXTSIZE, stdin)
Remember, stdin is a FILE* corresponding to the terminal, and fgets() stores the ‘\ ’ character at the
end of the string, before the ‘\\0’ terminator. The line will be truncated to maximum TEXTSIZE - 1
characters.
fgets() returns NULL when it encounters EOF.
Here is a sample user session. User input is highlighted with yellow.
3. Ensure the program compiles correctly and works correctly in Visual Studio. Test a few times.
4. Take a screenshot of the program after it completed, showing at least 3 cases of palindromes and 3
cases with non-palindromes, and with all the output displayed. Do NOT use the same input in the same
order as given above.
Solution
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf(\"Enter a string:\");
scanf(\"%s\", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf(\"%s is not a palindrome\", string1);
}
else {
printf(\"%s is a palindrome\", string1);
}
return 0;
}

