Problem in C Write a function is_palindrome() embedded in a program, that takes
ID: 3815740 • Letter: P
Question
Problem in C
Write a function is_palindrome() embedded in a program, that takes a pointer to the beginning of a string and returns a boolean value. That value is true if the string is a palindrome, false otherwise. The program is supposed to get a string from the user and pass it to the function.
You can use gets() to accomplish this, or, alternatively and more secure, fgets(). A call to fgets() would look something like that:
fgets(string, 80, stdin);
Be aware though that this function includes the newline character at the end of the string, which you will want to remove.
Hand in the C program file, commented *abundantly*.
Explanation / Answer
#include <stdio.h>
int is_palindrome(char *s) {
int n=0;
int i;
for(i=0; s[i]!=''; i++){
n++;
}
for(i=0; i<n; i++){
if(s[i] != s[n-1-i]){
return 0;
}
}
return 1;
}
int main()
{
char string[80];
int result;
printf("Enter the string: ");
fgets(string, 80, stdin);
result = is_palindrome(string);
if( result ==1){
printf("Given string is a palidrome ");
}
else{
printf("Given string is not a palidrome ");
}
return 0;
}
Output:
Enter the string: Suresh
Given string is not a palidrome
Enter the string: madam
Given string is a palidrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.