It is a sample C language example. It is a string palindrome program which accept a string from user.
It checks that accepted string is a palindrome string or not. If string or string reverse is as same as then this string is a palindrome string. It means read string same in both forward or backward direction.
Aim - C program to check whether the accepted string is palindrome or not.
Program Source Code
//C Program to check accepted string is palindrome or not
#include"stdio.h"
#include"conio.h"
int isPallindrome(char *);
void main()
{
int a;
char str1[30];
clrscr();
printf("\n******String Palindrome******\n");
printf("Enter a string: ");
gets(str1); // accept string from user
a=isPallindrome(str1); /*function calls, accept string as argument and return flag value*/
if(a)
printf("\nstring: %s is a palindrome string",str1);
else
printf("\nstring: %s is not a palindrome string",str1);
getch();
}
int isPallindrome(char *str2)
{
int i,j,flag=1;
for(i=0,j=strlen(str2)-1; i<j; i++, j--)
{
if(str2[i]!=str2[j]) /*starts checking from ends of string to middle*/
{
flag=0; /*if reverse of string is not same than return 0*/
break;
}
}
return(flag);
}
Output1
******String Palindrome******
Enter a string: MADAM
string: MADAM is a palindrome string
Output2
******String Palindrome******
Enter a string: HELLO
string: HELLO is not a palindrome string