Program to find the total number of palindrome in a given string

Thursday, December 4th, 2008

Here is  the program to find the total number of palindromes present in the entered sentence.  This program internally uses the logic to find the palindrome, discussed earlier.

Logic :  The given string is traced word-by-word till the EOL. To trace the words, it uses the logic of earlier program, where we counted the number of words. Each time, Each time, when we find the full word, we pass the ord to the internal block which checks if the word is a palindrome or not, the logic discussed earlier.

#include<stdio.h>
#include<conio.h>
int stpal(char str[50], int st, int ed);
void main()
{
char str[50];
int pal = 0, len = 0, i, start = 0, end;
clrscr();
printf(“nnt ENTER A SENTENCE…: “);
gets(str);
while(str[len]!=’′)
len++;
len–;
for(i=0;i<=len;i++)
{
if((str[i] == ‘ ‘ && str[i+1] != ‘ ‘) || i == len)
{
if(i == len)
end = i;
else
end = i – 1;
if( stpal (str, start, end ) )
pal++;
start = end + 2;
}
}
printf(“nnt THE TOTAL NUMBER OF PALINDROMES ARE..: %d”,pal);
getch();
}
int stpal(char str[50], int st, int ed)
{
int i, pal=0;
for(i=0; i<=(ed-st)/2; i++)
{
if(str[st+i] == str[ed-i])
pal = 1;
else
{
pal = 0;
break;
}
}
return pal;
}
Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 0 Comment


Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup