Recursion Palindrome

Pages: 12

I need help and don’t know what I’m doing


MFor this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function:

bool isPalindrome(string str, int lower, int upper)

that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is a palindrome. Test your 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.

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

using namespace std;

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

	if (str[low] != str[high])
		return false;

	return isPalindrome(str, low + 1, high - 1);

}

int main()
{
	string word;
	int stringLength str.length();

	cout << "Enter a word to see if its a palindrome" << endl;
	getline(cin, word);

	if (isPalindrome(str, 0, len - 1))
		cout << word << " : is a palindrome." << endl;
	else
		cout << word << " : is not a palindrome." << endl;

	return 0;

}
First, you should know that double posting is counter-productive.
The other thread: http://www.cplusplus.com/forum/beginner/267939/

If your problem is "I don't understand a thing", then you should go back to basics and study again.

1
2
3
4
5
6
7
8
9
10
11
bool isPalindrome(string str, int lower, int upper)
{
	if (low >= high)
		return true;

	if (str[low] != str[high])
		return false;

	return isPalindrome(str, low + 1, high - 1);

}


What is low? No such variable exists.
What is high? No such variable exists.

You can only use variables that exist.
Last edited on
ROFL cblack and ImageMan retweeter - the dream team. This thread will go on for weeks.
cblack618, don't worry about that poster. He's a racist troll who isn't bored yet. Just ignore his participation.

You're actually very close to having it working; just take a closer look at high and low and work out what you actually intended there.
Last edited on
So now we pull the racist card for no reason.

Even if it was true it sure as hell beats incompetence. ImageMan and blackness haven't soleved a problem yet - a combined total of zero runs on the board. after nearly 2800 posts. Not even 1 useful or creative line of code between the two.

Like I wrote, we're in for the long haul to nowhere from ImageMan. Nothing boring at watching the lack of intellect at play with the dream team matching-psychology at work.
Repeater,

Should my first if conditional statement be

If (str[low] >= str[high])

?
Here we go. Round 1.
cblack618 wrote:
Should my first if conditional statement be

If (str[low] >= str[high])


No. You should re-read Repeater's comment:
cblack618 wrote:
What is low? No such variable exists.
What is high? No such variable exists.
The problem has nothing to do with the condition. The problem is the variables. They do not exist. What variables are you trying to use here? What are their actual names?
Ohhh i see. I think I meant to put lower and upper in the conditional statement
black has him on the mat. Round 1 into its 4th hour now and no sign of blacks intelligence showing. Tag team dhayden shedding semblance of patience with a frustrated rage coming up.
Last edited on
Exactly so, cblack618.

Inside any given function, the following variables exist:

1) Parameters that were pass in to the function.
2) Variables you create within that function
3) Global variables that exist everywhere
4) In a class object, member variables of that class.

In this case, your function is very simple, and the variables that exist are only the parameters passed in; str, lower, and upper. As you surmise, you meant to use lower and upper.

With that, your palindrome function is complete and works!

1
2
3
4
5
6
7
8
9
10
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);
}


Good job!

Note, however, that you've made a similar mistake in your main function.

These line:
1
2
string word;
int stringLength = str.length();
(note that you missed the = in that second line there!)

In this line, you're trying to use a string named str to get the length, but your string was actually named word in the line above. Your logic is correct; you made a silly mistake there.

You're also trying to fetch the length of the string before the user entered it! Again, I can see the logic of what you're trying, the logic is correct, but you got it mixed up.

Here's the final, corrected version;

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
#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 the word from the user...
        string word;
	cout << "Enter a word to see if its a palindrome" << endl;
	getline(cin, word);

       // And THEN you can calulate the length of the word!
	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;

}


So well done, you got there, and that's what matters. Hopefully you've actually understood how to think about recursive functions. The basics are simple enough; they either return a value, or call themselves again with different parameters.

A shame the thread is interspersed by today's racist troll, but that's the internet for you, I suppose.
One change I'd make to the recursive function, pass the std::string as a reference. Passing as done now makes copies of the string with each call to the function. That could blow out the stack with all those replicated strings.

And make the passed string const.

bool isPalindrome(const std::string& str, int lower, int upper)
Hi Repeater,

Thank you for taking the time to help me and break things down and pinpoint to specific things I did wrong. I appreciate it
How would I 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.
?
Here we go Round 2

The winner of this round gets a free packet of pangolin powder to ward off those feelings of laziness, despondency and continuous whining.

https://www.nytimes.com/2020/02/10/science/pangolin-coronavirus.html
It so sad you have no life but to sit on the computer all day and troll and harass people. How about going getting you some money, getting some booty, or volunteering your misplaced time with a charity or something.
Consider passing the empty string word to
isPalindrome(word, 0, word.length() - 1)
Note that word.length() is unsigned.

If I'm not mistaken, the conversion from unsigned int back to int has unspecified behavior. How likely are we to find a computer system that doesn't produce -1 as the result of this conversion?
Last edited on
I help lots of people, see for yourself. Unlike ImageMan I pick questions I am competent to answer.

For you, perhaps some bat soup or civet pudding is more to your taste. They're coming up as freebies in the next 14 rounds of the inevitable Days of Our Jpegs match.

BTW Talking to anencephalic simians is in fact charity work.
mbozzi

How would i pass the empty string word to isPalindrome(word, etc)

What specific concept or prcoess is that ? I'm unsure
Pages: 12