Recursion Palindrome

Pages: 12
againtry,

You're so much of a bored troll that you constantly comment on my thread. Just overlook my threads and keep it moving.
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.
Last edited on
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.
Last edited on
ROFL rounds 3 and 4 done, 5 coming up :)
Day 2 with NoBrain
@jonnin solved this the whiner a couple of days ago. He can't even copy the solution.
I cant figure out how to get to enter strings terminaed by the ENTER key. I added a loop at line 24 but now i have a thousand errors

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
// Charles Blackwell CIS 221 M5
#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(string str, int lower, int upper)
{
	if (lower >= upper)
		return true;

	if (str[lower] != str[upper])
		return false;

	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 word empty 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 == ""
so what am i doing with getline and word =="" ?
Write a loop like this (exit the loop if the user enters an empty string):

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
#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
        {

        }
    }
}
Remember also that you cannot separate related instructions — the computer is too stupid to understand it that way.

C++ makes this pretty easy, though. Assuming any string, input terminated by a newline:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	string userInput;

	cout << "s? ";
	while (getline( cin, userInput ) and !userInput.empty())
	{
		if (isPalindrome( userInput, 0, userInput.length() - 1 ))
			cout << userInput << " : is a palindrome.\n";
		else
			cout << userInput << " : is not a palindrome.\n";
		cout << "s? ";
	}
}

That’ll get you as many strings as the user inputs until the input is the empty string.

Since the empty string is also a valid test case, you might want to require an end-of-stream to terminate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	string userInput;

	cout << "s? ";
	while (getline( cin, userInput ))
	{
		if (isPalindrome( userInput, 0, userInput.length() - 1 ))
			cout << userInput << " : is a palindrome.\n";
		else
			cout << userInput << " : is not a palindrome.\n";
		cout << "s? ";
	}
}

This only terminates when the user presses Ctrl+Z (Windows) or Ctrl+D (Linux) or EOF is reached (for redirected input).

Of course, you could just ask once and terminate:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	string userInput;

	cout << "s? ";
	getline( cin, userInput );

	if (isPalindrome( userInput, 0, userInput.length() - 1 ))
		cout << userInput << " : is a palindrome.\n";
	else
		cout << userInput << " : is not a palindrome.\n";
}

Hope this helps.

[edit] Fixed typos
Last edited on
thank you jlb

So Dutthomas what changes are i making to this code JLB gave 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
#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
        {

        }
    }
}
Last edited on
Well into day 2 now.
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.
Infighting by the self-appointed giants starts and it's only day 2.
Topic archived. No new replies allowed.
Pages: 12