help with palidrome missing one step







#include <iostream>
using namespace std;

 int main()
 {
   cout << "Enter a word to test if its a palidrome: ";
   string word;
   cin >> word;
   string Reverse;

   for (int x=word.size()-1; x>=0; x--)
   {
      Reverse = Reverse + word[x];

    if (word == 0)
        cout <<"The word is a palindrome."<<endl;
    else
        cout <<"The word is not a palindrome."<<endl;



   }
   cout <<Reverse ;
   return 0;
}




can anyone tell me what i am missing
Last edited on
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	bool isPalidrome = true;
	cout << "Enter a word to test if its a palidrome: ";
	string word;
	cin >> word;
	cin.ignore();
	string Reverse;

	for (int x = word.size() - 1; x >= 0; x--)
	{
		Reverse = Reverse + word[x];
	}

	for (int i = 0; i < word.size() - 1; i++)
	{
		if (word[i] != Reverse[i])
		{
			isPalidrome = false;
		}
	}

	if (isPalidrome)
		cout << word << " is a palidrome";
	else
		cout << word << " is not a palidrome";

	cin.ignore();
	return 0;
}
You seem to try to reverse the word, although in a clumsy way.
Then you check word == 0 inside the for() loop, which makes no sense.

As a proficient C++ programmer, you must not reinvent the wheel. In this case, you will use std::reverse() to reverse the string.
http://www.cplusplus.com/reference/algorithm/reverse/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <iostream>
#include <string> // missing in your code

// ...

std::string word;

std::cin >> word;

std::string reversed_word(word); // copy the word

std::reverse(reversed_word.begin(), reversed_word.end()); // reverse it

if (word == reversed_word)
    // say it is a palindrome, etc. 


Or a bit more elegantly, use std::reverse_copy() instead of std::reverse():
http://www.cplusplus.com/reference/algorithm/reverse_copy/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>
#include <iostream>
#include <string> // missing in your code

// ...

std::string word;
std::string reversed_word;

std::cin >> word;
reversed_word.resize(word.length()); // must resize to fit the contents
std::reverse_copy(word.begin(), word.end(), reversed_word.begin());

if (word == reversed_word)
    // say it is a palindrome, etc. 

Last edited on
As for Catfish, he is right in general, but if this is for homework, you should be reinventing the wheel, because that is the point, to learn about problem solving in computer science. Otherwise, do what Catfish said.
Or a bit more elegantly, use std::reverse_copy() instead of std::reverse():


Or, since you're making a copy anyway, just construct a new string in place with the reversed original:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

// ...

std::string word;
std::cin >> word;

std::string reversed_word(word.rbegin(), word.rend()) ;

if (word == reversed_word)
    // say it is a palindrome, etc.  
Or compare the first half of the string with the reverse of the second half:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <algorithm>

// ...

std::string word;
std::cin >> word;

if ( std::equal( word.begin(), word.begin + word.size()/2, word.rbegin() )
    // say it is a palindrome, etc.  
Topic archived. No new replies allowed.