Palindrome Number : If number is like 121 and reverse of that number is also same. So we can say that the number is palindrome.
Palindrome String : If string is like 'radar' and reverse of that string is also same then that string is known as palindrome string.
1) C program to check palindrome number
2) C program to check palindrome string using string library function
3) C program to check palindrome string without using string library function
1) C program to check palindrome number
#include <stdio.h>
int main()
{
int no, reverse = 0, temp;
printf("Enter No : ");
scanf("%d", &no);
temp = no;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp % 10;
temp = temp / 10;
}
if ( reverse == no )
printf("Palindrome Number");
else
printf("Not a Palindrome Number");
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[100], temp[100];
printf("Enter String : ");
gets(str);
strcpy(temp,str);
strrev(temp);
if( strcmp(str,temp) == 0 )
printf("String is palindrome");
else
printf("String is not palindrome");
return 0;
}
3) C program to check palindrome string without using string library function
#include<stdio.h>
int main()
{
char str[100];
int begin, middle, end, length = 0;
printf("Enter String : ");
gets(str);
while( str[length] != '\0' )
length++;
end = length - 1;
middle = length / 2;
for( begin = 0 ; begin < middle ; begin++ )
{
if ( str[begin] != str[end] )
{
printf("String is not palindrome");
break;
}
end--;
}
if( begin == middle )
printf("String is palindrome");
return 0;
}
0 comments :
Post a Comment