This is a C program to check a given string is palindrome.
This program accepts a string and checks whether a given string is palindrome.
1. Take a string as input and store it in the array.
2. Reverse the string and store it in another array.
3. Compare both the arrays.
Here is source code of the C program to check a given string is palindrome. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to read a string and check if it's a palindrome, without
* using library functions. Display the result.
*/
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
fflush(stdin);
printf("Enter a string \n");
gets(string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
for (i = length - 1; i >= 0; i--)
{
reverse_string[length - i - 1] = string[i];
}
/*
* Compare the input string and its reverse. If both are equal
* then the input string is palindrome.
*/
for (i = 0; i < length; i++)
{
if (reverse_string[i] == string[i])
flag = 1;
else
flag = 0;
}
if (flag == 1)
printf("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
1. Take a string as input and store it in the array string[].
2. Store the same string into the another array reverse_string[] in the reverse fashion.
3. Using for loop compare the elements of both the arrays.
4. If all the elements of the array are same, then it is a palindrome. Otherwise it is not a palindrome.
Enter a string sanfoundry sanfoundry is not a palindrome Enter a string malayalam malayalam is a palindrome
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy C Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Buy Computer Science Books
- Apply for Computer Science Internship