Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Given a string S, count the number of non empty sub strings that are palindromes.
A sub string is any continuous sequence of characters in the string.
A string is said to be palindrome, if the reverse of the string is same as itself.
Two sub strings are different if they occur at different positions in S

Input
Input contains only a single line that contains string S.

Output
Print a single number, the number of count in the string.

Constraints
1 <= |S| <= 50
S contains only lower case latin letters, that is characters a to z.

What I have tried:

C
#include <stdio.h>
#include<string.h>
int check(char s[],char a[],int x,int y)
{
    int i,p=0;
    for(i=x;i<=y;i++)
    {
        a[p]=s[i];
        p++;
    }
    a[p]='\0';
    int c=1;
    int j=0;
    while(j<=(strlen(a)/2))
    {
        if(a[j]!=a[strlen(a)-j-1])
        {
            c=0;
        }
        j++;
    }
    return c;
}
int main()
{
    char s[50];
    scanf("%s",s);
    char a[50];
    int i,j,c=0;
    for(i=0;i<strlen(s);i++)
    {
        for(j=i;j<strlen(s);j++)
        {
            int b=check(s,a,i,j);
            if(b==1)
            {
                c++;
            }
            
        }
    }
    printf("Number of palindromic substrings:%d",c);
  return 0;
}
Posted
Updated 21-Oct-17 11:33am
v2
Comments
OriginalGriff 21-Oct-17 8:59am    
And?
What does it do that your didn't expect, or not do that you did?
What have you tried to find out why it does that?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
Member 13474465 21-Oct-17 15:00pm    
it's not counting all the palindromes.when i count manually i can find more.
Patrice T 21-Oct-17 10:10am    
And you have a question or a problem?
Member 13474465 21-Oct-17 15:01pm    
problem,it's not counting all the strings
Patrice T 21-Oct-17 15:21pm    
Give an example !
Use Improve question to update your question.
So that everyone can pay attention to this information.

Quote:
problem,it's not counting all the strings

The first action is to add code to track what is doing your code:
C++
...
    while(j<=(strlen(a)/2))
    {
        if(a[j]!=a[strlen(a)-j-1])
        {
            c=0;
        }
        j++;
    }
    if (c==1)
    {
        printf("%s\n",a);
    }
    return c;
}

Then see which palindromes are found and which are not.

If the hint of the missing ones is enough, go to correct.
Otherwise, jump to debugger and see why it is missing a palindrome.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[update]
By the way, your algorithm is very naive, and very inefficient. The workload grow with the square of length of s, it is O(n²).
A little analyze show that any large palindrome is built around a smaller one until size 2 or 3. So if you don't have a small palindrome around a given center, you will not have a larger one around same center.
This means that with 'abcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh' you don't need to check large palindromes because you don't have small ones, this optimization is O(n).
 
Share this answer
 
v2
C++
void palindromeSubStrs(string s)
{
    map<string, int> m;
    int n = s.size();
 
    int R[2][n+1];
  
    for (int j = 0; j <= 1; j++)
    {
        int rp = 0;   
        R[j][0] = 0;
 
        int i = 1;
        while (i <= n)
        {
            while (s[i - rp - 1] == s[i + j + rp])
                rp++;   
            R[j][i] = rp;
            int k = 1;
            while ((R[j][i - k] != rp - k) && (k < rp))
            {
                R[j][i + k] = min(R[j][i - k],rp - k);
                k++;
            }
            rp = max(rp - k,0);
            i += k;
        }
    }
 
    s = s.substr(1, n);
 
    m[string(1, s[0])]=1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= 1; j++)
            for (int rp = R[j][i]; rp > 0; rp--)
               m[s.substr(i - rp - 1, 2 * rp + j)]=1;
        m[string(1, s[i])]=1;
    }
 
   cout << "There are " << m.size()-1
        << " palindrome sub-strings";
   map<string, int>::iterator ii;
   for (ii = m.begin(); ii!=m.end(); ++ii)
      cout << (*ii).first << endl;
}
 
int main()
{
    palindromeSubStrs("abaabbbba");
    return 0;
}
 
Share this answer
 
Comments
CPallini 21-Oct-17 11:40am    
Question is tagged with 'C'.
Patrice T 21-Oct-17 12:10pm    
What question did you answered ?
Michael Haephrati 21-Oct-17 14:58pm    
I answered this question, but missed the "C" tag. Sorry about that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900