Okay so someone may have already posted something like this but my assignment is due in like 4 hours and don't have time. So here is the problem and my code. My problem that I am having is that it only works for single words. Not whole sentences. I need the code to look somewhat like mine. Not with a bool or anything like that. Very similar to mine because book and what not is not something my book is into yet and my teacher would know. Even if I have taken several other programming classes.

A palindrome is a phrase that reads the same both forward and backward. Write a C++ program that reads a line from cin, prints its characters in reverse order on cout, and then pronounces judgement on whether the input line is a palindrome. Use the substr function within a loop to extract and print the characters one by one from the string, starting at the end of the string; at the same time, extract corresponding character starting at the beginning of the sting to compare with it. For example, here are two sample runs of the program:
Enter string: able was I ere I saw elba
able was I ere I saw elba
is a palindrome.
Enter string: madam I'm adam
madam I'm adam
is not a palindrome.

#include <iostream>
#include <string>

using namespace std; 

int main()
{
	string str; 
	int length; 
	string letter;
	string str2; 
	string first; 
	string last;
	string position;
	int numb; 
	int ber; 
	
	cout << "Enter word or sentence then press Enter." << endl; 
	cin >> str;   

	length = str.size() / 2;
	first =  str.substr(0, length); 
	last =  str.substr(length, '/n'); 
	numb = 0; 
	ber = 1; 

	cout << first << endl; 
	cout << last << endl; 
	
	while (numb <= length)
	{
		letter = last.substr(numb, ber); 
		cout << letter << endl; 
		str.substr(numb++, ber++); 
		str2 = letter + str2;
	}


	if (first == str2)
		cout << endl << str << " is a palindrome." << endl; 
	else 
		cout << endl << str << " is not a palindrome." <<endl; 


	system("Pause"); 
	return 0; 
}

Instead of

cin>>str

Use

gets(str);

std::string str
Error: no suitable conversion function from "std::string" to "char*" exists

Now it counts the spaces as part of it but it still says that its not a palindrome

Do you have to use substr()? Because this can be easily done with a for() loop.

#include <iostream>
using namespace std;

int main()
{
	string str;
	bool palindrome = true;

	cout << "Please enter a sentence: ";
	getline(cin, str);

	for( int i = 0, j = (int)str.length()-1; (i < (int)str.length() && j >= 0 && i <= j); i++, j--)
	{
		if( str[i] != str[j] )
		{
			palindrome = false;
			break;
		}
	}

	if( palindrome )
		cout << str << " is a palindrome!" << endl;
	else
		cout << str << " is not a palindrome!" << endl;

	return 0;
}

Have to use substr() yes. Part of the requirement and I cant do 'for' because my book has yet to cover it. I'd love to do it the easy way but I'd probably get docked for it. =/

Thanks for the help guys. I just ended up turning in what I had and she gave me full credit. Thanks much!

This article has been dead for over six months. Start a new discussion instead.