It's just a comment about a subtle error one could possibly encounter when checking to see if a string is a palindrome. The problem basically lies in the assignment. Don't worry about it.
Test the function by writing a main function that repeatedly asks the user to enter strings terminated by the ENTER key. These strings are then tested for palindromicity. The program terminates when the user presses the ENTER key without typing any characters before it.
Step 1. Write a main function that repeatedly prints the text Hello World to the screen, forever.
Step 2. Post the resulting main function here.
// Charles Blackwell CIS 221 M5
#include <iostream>
#include <string>
usingnamespace std;
bool isPalindrome(string str, int lower, int upper)
{
if (lower >= upper)
returntrue;
if (str[lower] != str[upper])
returnfalse;
return isPalindrome(str, lower + 1, upper - 1);
}
int main()
{
// Get word from user
string word;
while (word != '\r')
{
word = "";
}
cout << "Enter a word to see if its a palindrome" << endl;
getline(cin, word);
//If else statement to calcualte the lenght of the string
int stringLength = word.length();
if (isPalindrome(word, 0, stringLength - 1))
cout << word << " : is a palindrome." << endl;
else
cout << word << " : is not a palindrome." << endl;
return 0;
}
The call getline(cin, word)
Leaves wordempty if the user does not type any characters before pressing enter.
You can test to see if a string is empty by comparing it with the empty string: word == ""
#include <iostream>
#include <string>
bool isPalindrome( std::string str, int lower, int upper ) ;
int main()
{
std::string str ;
// repeatedly ask the user to enter strings terminated by the ENTER key.
// The program terminates when the user presses the ENTER key without typing any characters before it.
while( std::cout << "enter a string (enter an empty string to end the program): " &&
std::getline( std::cin, str ) &&
!str.empty() ) // exit from the loop if the string is empty
// ie. the user pressed the ENTER key without typing any characters before it
{
// check if str is a palindrome lower == 0, upper == position of the last character in the string
if( isPalindrome( str, 0, str.size()-1 ) ) // invariant: str.size() != 0 (it is not an empty string)
{
// ...
}
else // not a palindrome
{
}
}
}
#include <iostream>
#include <string>
bool isPalindrome( std::string str, int lower, int upper ) ;
int main()
{
std::string str ;
// repeatedly ask the user to enter strings terminated by the ENTER key.
// The program terminates when the user presses the ENTER key without typing any characters before it.
while( std::cout << "enter a string (enter an empty string to end the program): " &&
std::getline( std::cin, str ) &&
!str.empty() ) // exit from the loop if the string is empty
// ie. the user pressed the ENTER key without typing any characters before it
{
// check if str is a palindrome lower == 0, upper == position of the last character in the string
if( isPalindrome( str, 0, str.size()-1 ) ) // invariant: str.size() != 0 (it is not an empty string)
{
// ...
}
else // not a palindrome
{
}
}
}
the computer is too stupid to understand it that way
Now, if I was really a troll, I could make very good use of that very unfortunate turn of phrase by way of a comparison with the one-who-whines and how that would be a race to the bottom the computer would lose.
JLBorges and I posted about the same time -- he was just faster. His code is the same as my first one, just formatted a bit differently and with a better prompt.
For future reference, you can simply compile the different versions and see how they behave.