c code to check if a string is a palindrome or not and for palindrome number. The first step we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not palindrome.Some palindrome strings examples are “dad“, “radar“, “madam” etc.
Input:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0,k=0;
char *str;
char *s;
clrscr();
printf("Program to find whether the given string and it's reverse are same\n\n");
printf("Enter the length of the string\n");
scanf("%d",&j);
str=(char*)malloc((j+1)*sizeof(char));
s=(char*)malloc((j+1)*sizeof(char));
fflush(stdin);
printf("Enter the string\n");
gets(str);
for(i=0;i<j;i++)
{
*(s+i)=*(str+(j-1-i));
//printf("Character %d of Original string=%c\n",i,*(str+i));
//printf("Character %d of string=%c\n",i,*(s+i));
}
for(i=0;i<j;i++)
{
if(*(s+i)==*(str+i))
{
k++;
}
}
if(k==j)
{
printf("\nAll characters of the entered string and it's reverse are same\n");
printf("\nHence the entered string can be called as a PALINDROME\n");
}
else
{
printf("All characters of the entered string and it's reverse are not same\n");
}
getch();
}
Input:
Program to find whether the given string and it's reverse are same
Enter the length of the string
5
Enter the string
seves
Output :
All characters of the entered string and it’s reverse are same
Hence the entered string can be called as a PALINDROME
Enter the length of the string
5
Enter the string
seves
Output :
All characters of the entered string and it’s reverse are same
Hence the entered string can be called as a PALINDROME
Comments
Post a Comment