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:
- Get the input string from the user.
- Compare one element with all other elements in the string.
- When the elements are matching take the complete length as a string.
- Then check the word with palindrome matching.
- And print all palindromes present in the word.
Program:
#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(); }