Showing posts with label palindrome. Show all posts
Showing posts with label palindrome. Show all posts

Sunday 8 July 2012

Find the number of substrings of a string that are palindromes


#include<stdio.h>
#include<string.h>

int is_palindrom(char S[20],int f,int l)
{
    for(;f<=l;f++,l--)
    {
        if(S[f]!=S[l])
            return 0;
    }
return 1;
}
int main()
{
    char S[20];
    int i,l,j,k,count=0;
    gets(S);
    l=strlen(S);
    for(i=0;i<l;i++)
    {
        for(j=i+1;j<l;j++)
        {
            if(S[i]==S[j])
                if(is_palindrom(S,i,j))
                {
                    printf("\n");
                    count++;
                    for(k=i;k<=j;k++)
                        printf("%c",S[k]);
                }
        }
    }
    printf("\n\nTotal << %d >> no of palindrom in given string\n",count);
return 0;
}

Let me know your thoughts.