Write only in C code, not C++ please. Problem 1 : Palindromes A palindrome is a
ID: 3574949 • Letter: W
Question
Write only in C code, not C++ please.
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 comma and newlines " ' t', comma ', ' and Write in a file p1.c a function called isPalindrome0 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 ' O' 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. (https://www.tutoriaspoint.com/c standard library/c function isspace.htm) that strings are case sensitive - so there is no need to convert the string to the same case Write in file pl.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 [TEXTSIZEJ from the terminal with function fgets(), use the function call: Remember, stdin is a FILE* corresponding to the terminal, and fgets() stores the ' n' 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.Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isPalindrome(char string1[]){
char c;
int i, length, result,j,tLength;
int flag = 0;
char temp[100];
length = strlen(string1);
j=0;
for(i=0;i < length ;i++){
c = string1[i];
result = isspace(c);
if (result == 0)
{
temp[j] = c;
j++;
}
}
tLength = strlen(temp);
for(i=0;i < tLength ;i++){
if(temp[i] != temp[tLength-i-1]){
flag = 1;
break;
}
}
return flag;
}
int main(){
char string1[100];
printf("Enter a string:");
fgets(string1, 100, stdin);
int flag = isPalindrome(string1);
if (flag) {
printf(" %s is not a palindrome", string1);
}
else {
printf(" %s is a palindrome", string1);
}
return 0;
}
OUTPUT:
Enter a string:never odd or even
never odd or even
is a palindrome
Process returned 0 (0x0) execution time : 20.456 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.