Palindrome
A string which reads the same, when it is read forward and backward is called a palindrome.
Eg - ANNA
Explanation of the program
This program reads a string as input, checks whether it is palindrome or not and prints the result. Initially the variable i is set to 0. Input string is read into character array a[]. The length of the string if found by using the string handling function strlen() and is stored in the variable j. Now the values at a[i] and a[j] is compared in the while loop, at each iteration, the value of i is incremented and the value of j is decremented. At each iteration, if the value of a[i] and [j] are equal then the string is palindrome, otherwise the string is not palindrome.
Program
//palindrome program in c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int flag=0,i=0,j;
char a[10];
printf("Enter the string\n");
scanf("%s",a);
j=strlen(a)-1;
while(a[i]!='\0' && j>=0)
{
if(a[i]!=a[j])
{
flag=1;
printf("Not palindrome");
break;
}
i++;
j--;
}
if(flag==0)
{
printf("palindrome");
}
getch();
}
Output
C program to check whether a string is palindrome |
No comments:
Post a Comment