I can't make this search for and count for palindrome works. :(

Pages: 12
Hi guys,

I need your help. Can you check my code whats wrong with my check function and palindrome check. Palindrome is like a word that even if you read it backwards its still the same. Like "baab" "saas" "ckykc". Thank you to those who can help me out :(

Last edited on
LINE 54 variable i being used without being initialized

replace LINE 33 with int len, space,counter, i = 0;

I'm not shore if that would be smart since I din't go into "logics" of you program.
setting i to 0 however works
Well it needs to check the isPalindrome(eachWord) in the check function. If a word is a palindrome or not. And looks like my code doesn't have the correct style of checking the count of words to be used for checking a palindrome.

EDIT:

Alright I checked my codes and the check function won't even count the words that I tried in line 40

Does anyone know how to count the words in a string?
Last edited on
.
Last edited on
Amount of words or characters? Characters is easy, just use strlen().
Oh my bad. to find the number of words in a string just count the number of spaces.
Cycle through the string and if you find a space then add that to a counter.
oh wait. But there is a problem. Well I would like to like seperate words instead not counting them. Cause what I'm trying to do here is like if you put in "Need help in palindrome is an example of eye, boob". In that case. The only bold letters should be counted as palindromes. Hope you get my point on this.

Palindrome is a word that even if you read it backwards its still has the same position of the char string. Like eye, boob, ana, deed, pasap

Thanks. :(
Last edited on
Ok let me give it a shot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string line = "line of words";
list<string> stringList;
string tempWord = "";
int x = 0;
int y = 0;

for( x; x < line.length(); x++ )
{
     // if the program finds a space
     if( line[x] == ' '  )
     {
          for( y; y < x; y++ )
          {   // add each individual character up to the space
               tempWord.push_back(line[y];
          }
          // push back the selected word into the list
         stringList.push_back(tempWord);
         // skip past the space
         x++;
     }
}


That is a rough guess at separating the words. But you will have a word left over since this method selects string up to the spaces and not after. So you just have to add the final characters of the string into a word.
Ok. Its really hard though. I don't know what to do. Thanks for your help though :)
Well we can find the problems and work this out! Did you test out the code to separate words?
Theres an error with the line 2 of your script. I don't know how this list works
Oh, you have to include <list>.
Can you give an example of a complete code. I'm sorry. I didn't get it :( Thank you for your help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string line = "line of words";
<list>list<string> stringList;
string tempWord = "";
int x = 0;
int y = 0;

for( x; x < line.length(); x++ )
{
     // if the program finds a space
     if( line[x] == ' '  )
     {
          for( y; y < x; y++ )
          {   // add each individual character up to the space
               tempWord.push_back(line[y]);
          }
          // push back the selected word into the list
         stringList.push_back(tempWord);
         // skip past the space
         x++;
     }
}


Like that?
No, <list> is like <iostream>, you have to include it at the beginning of your code.
#include <list>
oh ok i get it :)
Thats weird. I put cout at the end to show the value of x. Value of x is 5 and 8.
Oh yeah cause the space is found on the 5 str and 8 string. But my problem now is how will I ever check every single word I put. like for example "line of words" has 3 words and it will check every single word. :)
Ok i put this into practice and i messed up.

You need to do y++ instead of x++ to skip past the space, and please post your code.
Here's mine right now.


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <list>

using namespace std;

/*bool isPalindrome(string eachWord)
{
    eachWord = tolower(eachWord[eachWord.length()]);
    for(int i=0;i<eachWord.length()/2;i++)
    {
        if(eachWord[i]!=eachWord[eachWord.length()-1-i])
        return false;
    }
    return true;
}*/

//determine if word is a Palindrome or not
bool isPalindrome(string eachWord)
{
  string str(eachWord);
  string::reverse_iterator rit;
  for ( rit=str.rbegin() ; rit < str.rend(); rit++ )
    cout << *rit;
  return 0;
}

//checks the string
bool check(string words)
{
    int len, space,counter, i;
    string newSet,eachWord;
    for(counter=0; counter<words.length();counter++)
    {
        do{
        space = words.find(" "||"'"||","||"."||"!"||"?");
        eachWord = words.substr(0, space);
        len = eachWord.length();
        isPalindrome(eachWord);
        newSet += words.substr(space+1);

        if(isPalindrome(eachWord))
        {
            i=(len/len)-1;
            i++;
        }
    }while(space!=string::npos);
    }
    cout << "No. of Palindrome/s: " << i << endl;
    /*isPalindrome(words);
    if(isPalindrome(words))
        cout << "No. of Palindrome/s: 1" << endl;*/
}

int main()
{
    string words;
    cout << "Input words:" << endl;
    getline(cin, words);

list<string> stringList;
string tempWord = "";
int x = 0;
int y = 0;

for( x; x < words.length(); x++ )
{
     // if the program finds a space
     if( words[x] == ' '  )
     {
          for( y; y < x; y++ )
          {   // add each individual character up to the space
               tempWord.push_back(words[y]);
          }
          // push back the selected word into the list
         stringList.push_back(tempWord);
         // skip past the space
         x++;
         cout << x << endl;
     }
}

    check(words);
//    return 0; // This doesn't do anything.
}
Last edited on
Pages: 12