To check whether the given string is palindrome-method 2

Here is another program, advanced version of the previous program, to check if the entered string is a palindrome. Palindrome is a string segment, which reads same from both the directions. The same method is implemented here in the program also.

Logic : The given string is referred by two names, where one is traced by the SOL (Start Of Line) and the other is traced from EOL (End Of Line). In every iteration, the condition is checked, if both the characters are the same. If so, sets a flag, “pal” as true (i.e. 1), and hence prints out the result.

The problem can also solved in another method, where we check by reversing the string and then comparing.

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also c/c++, java programs.

#include
main()
{
char a[20],b[20];
printf(“enter a string”);
scanf(“%s”,&a);
strcpy(b,a);//copies string a to b
strrev(b);//reverses string b
if(strcmp(a,b)==0)//compares if the original and reverse strings are same
printf(“\n%s is a palindrome”,a);
else
printf(“\n%s is not a palindrome”,a);
return 0;
}

#include
#include//this headel file is important
main()
{
char a[20];
char b[20];
printf(“enter a string”);
gets(a);
strcpy(b,a);//copies string a to b
strrev(b);//reverses string b
if(strcmp(a,b)==0)//compares if the original and reverse strings are same
printf(“\n%s is a palindrome”,a);
else
printf(“\n%s is not a palindrome”,a);
return 0;
}

You need to include the header file to get the desired output!#include
#include
#include
main()
{
char a[20];
char b[20];
printf(“enter a string”);
gets(a);
strcpy(b,a);//copies string a to b
strrev(b);//reverses string b
if(strcmp(a,b)==0)//compares if the original and reverse strings are same
printf(“\n%s is a palindrome”,a);
else
printf(“\n%s is not a palindrome”,a);
return 0;
}

Subscribe

About Electrofriends

This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.