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;
}