How to find Number of Palindromes

I have made the following C++ code, It tells me if a String is Palindrome or not. How do I make it to work that it tells me how many different Palindromes are there in the string. Again I don't know any high level coding. Please tell the solution which is appropriate for a beginner. I know the fucntions of the following header files only: iostream, conio, stdio,math, string(only strcpy and strcmp)

Example: //The Output of the Present code:
Enter String: DAD
It is a Pallindrome
OR
Enter String: DAP
It is not a Palindrome

//what i want to make it to:
Enter String: DAD MOM POP
Number of Palindromes - 3
OR
Enter String: DAK LAKE
Number of Palindromes -0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
        clrscr();
        char x[20],y[20];
        int z=0;
        cout<<"\nEnter a String: ";
        gets(x);
        strrev(x)=y[20];
        for(int i=0;i<20;i++)
        {
         if(x[i]==y[i])
         {
                z++;
         }
        }
        if(z>0)
        {
                cout<<"\n\nIt is a Pallindrome";
        }
        else
        {
                cout<<"\n\nIt is not a Pallindrome";
        }
        getch();
}
You should rethink the logic. Try the string ABCA. The reverse is ACBA. x[0]==y[0] and x[3]=y[3], but x[1]!=y[1] and x[2]!=y[2]. So in your code z=2
can't understand
Does your program work? Look at line 12. Then try with the ABCA string (not a palindrome)
remember that:
- every string of length 1 is a palindrome i.e. a is palindrome, b is palindrome, ...

- a palindrome is a string of palindromes i.e. aba is a string containing a, b, aba
racecar is a string containing a, c, e, r, cec, aceca, racecar, ...
Last edited on
It does look like the OP actually wants to type a list of words and expects as result the number of words that are palindromes. That is different from dissecting a word to substrings. "foo abba oof" contains one: "abba".

Substrings: dissecting 'acaca' returns 4; 'acaca', 'aca', 'cac', 'aca'.

Third interpretation ignores whitespace and concatenates words of the "string": 'innostunut sonni'


1
2
3
char y[20];
gets(x);
strrev(x) = y[20]; // What? 
Topic archived. No new replies allowed.