To print all palindromes present in a word

To print all palindromes present in a word

This program can able to print all palindromes present in a word and also its count.

Logic:

  • Get the input string from the user.
  • Then compare one character with all other characters.
  • If the character is matching take it as a word.
  • And check the word whether it is palindrome or not.

Algorithm:

  1. Get the input string from the user.
  2. Compare one element with all other elements in the string.
  3. When the elements are matching take the complete length as a string.
  4. Then check the word with palindrome matching.
  5. And print all palindromes present in the word.

Program:

[code lang=”c”]
#include<stdio.h>
void main()
{
int n,i,j,k,b,count=0,l,e,g;
char a[20];
clrscr();
printf("enter the string:");
gets(a);
n=strlen(a);
if(n==1)
{
printf("The palindrome is:%c",a[0]);
printf("\n");
}/*each element checks with all other elements and if they matches then check whether it is palindrome or not*/
for(i=0;i<n;i++)
{ l=i+1;
for(j=i;l<n;)
{
if(a[j]==a[l])//if one element equals with other
{ e=l;
for(k=j;k<=l;k++)//checks palindrome or not
{
if(a[k]!=a[e])
{
break;
}
if(k>=(l+1)/2)// print the palindrome strings
{
count++;//count no of palindrome
printf("The palindromes are:");
for(g=j;g<=l;g++)
{
printf("%c",a[g]);
}
printf("\n");
break;
}
e–;
}
} l++;
}
}
printf("No of palindrome is:%d",count);
getch();
}
[/code]

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *