Hi viewers , today are going to look at string palindrome .
Just consider a word , say hello . When you reverse it you get olleh . The initial word and the reversed word or not the same. When you reverse the word Malayalam , you get the same word . This is called string palindrome.
Let's write a C program to find if the string accepted from the user is a palindrome or not . To compile this you need a C compiler If you don't have a compiler get it from here.
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],rev[100]; //declare strings
printf("Enter the string");
gets(str); // accept string str from user
strcpy(rev,str); // copying string from str to rev
strrev(rev); //reverse the string rev
if((strcmp(rev,str))==0) //if else condition to check str and rev are same
{
printf("\nThe given string is a palindrome");
}
else
{
printf("\nThe given string is not a palindrome");
}
return 0;
}
What is string palindrome?
Just consider a word , say hello . When you reverse it you get olleh . The initial word and the reversed word or not the same. When you reverse the word Malayalam , you get the same word . This is called string palindrome.
Let's write a C program to find if the string accepted from the user is a palindrome or not . To compile this you need a C compiler If you don't have a compiler get it from here.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],rev[100]; //declare strings
printf("Enter the string");
gets(str); // accept string str from user
strcpy(rev,str); // copying string from str to rev
strrev(rev); //reverse the string rev
if((strcmp(rev,str))==0) //if else condition to check str and rev are same
{
printf("\nThe given string is a palindrome");
}
else
{
printf("\nThe given string is not a palindrome");
}
return 0;
}
Output:
Sample output 1:
Sample output 2:
Hope you would have understood the concept of string palindrome.
Check out other C programs:
- To check if the given number is an Armstrong number
- To find the sum of digits
- To find the sum of square series.
We value your suggestions and feedback.
Subscribe to our blog to get posts by mail.
Fill out the subscription form on our page.
Share it with your friends in social media
No comments:
Post a Comment