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 :(
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?
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
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.
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++;
}
}
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. :)
#include <iostream>
#include <string>
#include <list>
usingnamespace 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.
}