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

Pages: 12
what is the problem?
To make it simple. First is I want the check function that would check for every word if it is a palindrome. Well palindrome are words that even if you read it backwards its still the same.

Sample of those words: eye, matam, krark, dad, soapaos, ete, siiiiis.
I know what a palindrome is lol, but what is the problem with the code?
Then if a certain word is considered as a palindrome then it would be counted as 1 palindrome then if another palindrome word is found on the string given then it would be counted as another one.
From my code? It doesn't work at all.
Does it compile? If not then what is the error. If it does then what is the program doing wrong?
If I tried putting a word haoh. It still counts as one. But its not a palindrome. Then the next bug is even if i put "I can't make this work" or more than one word is being considered as one word.
This code separates a string of words:

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
#include <iostream>
#include <list>
#include <string>

using namespace std;

int main()
{

	string line = "line of words";
	list<string> stringList;
	string tempWord = "";

	int x = 0;
	int y = 0;

	for( x; x < signed(line.length()); x++ )
	{
		// if the program finds a space
		if( line[x] == ' '  )
		{
			cout << "\nFound a space!\n" << endl;

			for( y; y < x; y++ )
			{   // add each individual character up to the space
				std::cout << "Adding the letter: " << line[y] << endl;
				tempWord.push_back(line[y]);
				std::cout << "Temp word: " << tempWord << endl << endl;
			}
			std::cout << "finished word: " << tempWord << endl << endl;
			// push back the selected word into the list
			stringList.push_back(tempWord);
			tempWord = "";
			// skip past the space
			y++;
		}
	}

	// get the remainder of characters after the final space
	for( y; y < signed(line.length()); y++ )
		tempWord.push_back(line[y]);

	stringList.push_back(tempWord);

	list<string>::iterator s = stringList.begin();
	for( int x = 0; x < stringList.size(); x++ )
	{
                // display the separated words
		std::cout << *s << std::endl;
		s++;
	}

	std::cin.ignore().get();
	return 0;
}
Last edited on
Wow. This is great. But when I try this code the last word is not being included in the loop where being checked letter by letter. What does std:: do?
Still can't make this work. :( Can anyone do the code for me please? :(
> Can anyone do the code for me please?

Yes. But would you be able to submit it?

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
#include <locale>
#include <string>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <cctype>
#include <iostream>

bool punctuation( char c ) { return std::ispunct( c, std::cin.getloc() ) ; }

bool palindorome( const std::string& str ) // note: case sensitive
{ return std::equal( str.begin(), str. begin() + str.size()/2, str.rbegin() ) ; }

int main()
{
    std::string line ;
    while( std::cout << "words? " && std::getline( std::cin, line ) )
    {
        std::replace_if( line.begin(), line.end(), punctuation, ' ' ) ;
        std::istringstream stm(line) ;
        std::istream_iterator<std::string> begin(stm), end ;
        std::cout << "#palindromes: "
                  << std::count_if( begin, end, palindorome ) << '\n' ;
    }
}

I don't get your coding :(
Im trying to make a isPalindrome function check but when I compile it says str::string cannot convert to const char. :|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isPalindrome(string eachWord)
{
    int x = strlen(eachWord)-1;
    for(int i = 0; i <= x; i++)
    {
        if (eachWord[i] == eachWord[x-i]) {
            cout<<"palidrome"<<endl;
            return 1;
        }
        else {
            cout<<"Not a palidrome"<<endl;
            return 0;
        }
    }
}
JLBorges, your code works completely what I want. But it's too advance and I can't even understand how some of your codes work :(
This is what JLBorges meant by:
Yes. But would you be able to submit it?


You need to learn the basics before you can optimize code... JL has used in built functions from various header files to make his code work.. While learning basic, you need to figure out how these functions work, and how you can manually write them.. People here are ready to help you out, why try and get your work done from them?

Just out of curiosity, if the return type of your function is bool, shouldn't it return true/false instead of 0/1? Also, in your for loop, why check till the end of the loop? Even if you check till the halfway mark (x/2 for even number of letters, and (x-1)/2 for odd), doesn't it mean the rest half is same? It'll increase the efficiency of your code..

Secondly, your code will give the following output for a sample word "hasbah"
palindrome
palindrome
not a palindrome
not a palindrome
palindrome
palindrome


That's clearly not what you want, do you? This is not a compiler error, but a logical error. Fix it first.. Try something like:
1
2
3
4
5
6
7
8
9
10
11
12
bool isPalindrome(string eachWord)
{
    int x = strlen(eachWord)-1;
    bool isPrime=true;
    for(int i = 0; i <= x; i++)
    {
        if (eachWord[i] != eachWord[x-i]) {
            isPrime=false;
        }
    }
    return isPrime;
}


Can't figure about the const char thing.. Maybe someone else can help you with that..
Last edited on
Thanks for your reply. Thats what I'm trying to figure out as of now. And still can't make this work :(

I tried this code instead:
1
2
3
4
5
6
7
8
9
10
11
12
bool isPalindrome(string eachWord)
{
    int x = eachWord.length()-1;
    bool isPrime=true;
    for(int i = 0; i <= x; i++)
    {
        if (eachWord[i] != eachWord[x-i]) {
            isPrime=false;
        }
    }
    return isPrime;
}
Last edited on
How can I make this not-case sensitive when checking? I tried looping so that I could use tolower(line[]) but it doesn't work.

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
#include <locale>
#include <string>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <cctype>
#include <iostream>

bool punctuation( char c ) { return std::ispunct( c, std::cin.getloc() ) ; }

bool palindorome( const std::string& str ) // note: case sensitive
{ return std::equal( str.begin(), str. begin() + str.size()/2, str.rbegin() ) ; }

int main()
{
    std::string line ;
    while( std::cout << "words? " && std::getline( std::cin, line ) )
    {
        std::replace_if( line.begin(), line.end(), punctuation, ' ' ) ;
        std::istringstream stm(line) ;
        std::istream_iterator<std::string> begin(stm), end ;
        std::cout << "#palindromes: "
                  << std::count_if( begin, end, palindorome ) << '\n' ;
    }
}
I'm almost done! Please help me! Its already working. But now my problem is we haven't studied the iterators and stream. Now I need how to make this bold ones to basic scripting. Please help me :(

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool isPalindrome (const std::string& s) {
    for (int i = 0; i < (s.length())/2; ++i) {
        if (s[i] != s[s.length()-1 - i])
            return false;
        else
            return true;
    }
}

bool checkLetter(char c) {
    return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); //Checks if letter or punctuation
}

bool checkPalindrome(const string & s) {
    string::const_iterator it = s.begin(); //Iterator from the beginning
    string::const_reverse_iterator rt = s.rbegin(); //Reverse iterator coming from the end
    while (it != s.end() && rt != s.rend() && distance(it, rt.base()) > 0) { //stops when "it" has gone past the "rt"
        //Skip non-letters
        while (it != s.end() && !checkLetter(*it))
            ++it;
        while (rt != s.rend() && !checkLetter(*rt))
            ++rt;
        if (it == s.end() || rt == s.rend())
            break;  //isPalindrome
        if (tolower(*it) != tolower(*rt))
            return false;  //Case insensitive
        ++it;
        ++rt;
    } return true;
}

int main() {
    string phrase;
    int length = phrase.length();
    int counter=0;
    int i=0;
    cout << "Enter a word: " << endl;
    getline(cin, phrase);
    stringstream tokens(phrase);
    for(string word; tokens >> word;)
        if(checkPalindrome(word))
            ++counter;
    cout << "No. of Palindromes: " << counter << endl;
    return 0;
}
Last edited on
> Its already working.

If it is working correctly, and you understand how it is working (you can explain the logic to your teacher), there is no reason to look for anything else - submit it as it is.

> But now my problem is we haven't studied the iterators and stream.
> Now I need how to make this bold ones to basic scripting. Please help me :(

I suppose you have not been explicitly forbidden from learning anything on your own. Your teacher should be quite pleased with you for that.

If you have a teacher who is unhappy with students learning more that what has been covered in the classroom, what you need to change is your teacher - not the program you have written.


Topic archived. No new replies allowed.
Pages: 12