Recursive Functions

Pages: 123
We'll take a string:
someInputString
We will compare the first and last letters:
someInputString

Is there something about that comparison, about those two letters, about the s and the g , that tells you if the word is a palindrome or not?

This is what we've come back to; if you don't understand how to look at a word and figure out if it's a palindrome or not, you'll never be able to write code to do it. You need to know how to look at a word and by looking at it, figure out if it's a palindrome or not. Only once you know how to do that can you hope to code it.
Last edited on
The point is that your lines 36-40 will never be called.

IF condition is true, then the function ends on line 30
ELSE the function ends on line 34

There is no way to reach line 36.


Lets try isPalindrome( "Ha", 0, 1 )
1. 0 is less than 1, so there is a call to isPalindrome( "Ha", 1, 0 )
2. 1 is greater than 0, and thus the result is true.
Is Ha really a palindrome?
Last edited on
I don’t know what I’m doing.

I’m not expecting anyone to write my whole code or do my whole assignment. I’m willing to learn but I do need help and would be great if someone can break it down into simple basic steps.
Okay, well, when you look at a word to see if it's a palindrome, what you can do is look at the first and last letter.

If they're NOT the same, then the word isn't a palindrome.

Let's start with some example two letters words.

aa
So, the first letter is a, and the last letter is a, so this IS a palindrome.

Let's try another.

gg
So, the first letter is g, and the last letter is g, so this IS a palindrome.

Let's look at a third:
ar

The first letter is a, and the last letter is r. a is NOT the same letter as r, so this one is NOT a palindrome.


Okay, what about longer words? Well, after checking the first and last letter, then you check the second and second-to-last letter.

abba
The first letter is a, and the last letter is a. Okay, this could be a palindrome! Now, check the second and second-to-last letter. The second letter is b, and the second-to-last letter is b. Hey, they're the same! b is the same letter as b! So this is a palindrome.

Let's try another.

abca
The first letter is a, and the last letter is a. Okay, this could be a palindrome! Now, check the second and second-to-last letter. The second letter is b, and the second-to-last letter is c. Hey, they're NOT the same! b is NOT the same letter as c! So this is NOT a palindrome.

How about a bigger word? Same deal, check the first and last letter, and then the second and second-to-last letter, and then the third and third-to-last-letter...

So try this one. You tell me; is this word a palindrome?

abcdb
No. That word isn’t a palindrome
Brilliant. How did you know?

The procedure I described above is what you're trying to code.
Last edited on
Cause the first and last word wasn’t the same. Also the second and second to last wasn’t the same.

So the problem is: I might can you tell you what a concept is by definition or what is it purpose but I don’t know how to actual put it to practice via actual code
Comparing first and last letter of a string, of size X.
string[0] == string[X-1];

Comparing second and second to last letter:
string[1] == string[X-2];


Comparing third and third to last letter:
string[2] == string[X-3];


See the pattern? Each time, the function compares two letters of the string.
The first and last.
Then the second and second to last.
Then the third and third to last...
And so on.
I understand just a tad. I’m still not sure what else I need to do specifically to my assignment
You need to look at the first and last letter, and if they're the same, keep going by looking at the next pair, and the next, and the next, and the next, and if you run out of letters before you find a pair that don't match, return true.

If you ever find a pair that don't match, return false.

Every look at a pair is a whole new call to the function.
Last edited on
But I’m not sure how to turn what you’re telling me into actual C++.

Can you look at my code and point me to a specific line as to where I need to start and what I should be doing at that specific line ?
Ok, on the grounds that this is just a couple of lines of code, and that recursion isn't easy the first time or two, and you've tried at it all day, and its already on the web for the taking, Im going to give you something to work with. This won't work if you want to accept things like "a dog a panic in a pagoda" because spaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

bool isPalindrome(string str, int lower, int upper)
{
	if(upper <= lower)  //this was right.  most everything else was not. 
	return true;  
        	
        if (str[lower] != str[upper])  //you have to check the letters against each other and have a way to return not a palindrome, right?
         return false;  
        return(isPalindrome(str, lower+1, upper -1)); // recursive call.  note how the indices need to be passed in with an alteration. 
}

int main()
{
  string p = "abcdecba";
  string p2 = "abcdcba";
  string p3 = "abccba";
  cout << isPalindrome(p,0,p.length()-1) << endl;
  cout << isPalindrome(p2,0,p2.length()-1) << endl;
  cout << isPalindrome(p3,0,p3.length()-1) << endl; 
}


study it until you understand it or you won't ever get this stuff.
Last edited on
Ok, on the grounds that this is just a couple of lines of code, and that recursion isn't easy the first time or two

But that's what happens every time with this person.
He was just waiting for you to write the code for him.
@jonnin Excellent work! Truly one of the best contributions I've seen here ;)
Dutch

Dude seriously screw off. I’m waiting for any one to write code or do my whole assignment. If that was the case I would of just posted the assignment instructions and said “help me do this” with no code.

So please spare me of the trolling, harassment and condescending remarks. I’ve stated from the very beginning. I’m a B-E-G-I-N-N-E-R. What part of that don’t you understand ?

If I knew what I were doing why would I bother with this forum of getting constant smart remarks from trolls like yourself ?
Uhhhh you bothered responding to MY thread. You screw off
Dude seriously screw off.

@jonnin gave you very good help - one of the best I've seen on recursion anywhere.

You got what you wanted. But not a word of thanks from you, and no green tick.

So, it's a 'fuck off cblack618' from me too. Seriously.

Dude,
I’m very thankful for any help anyone gives me on here. So pump your brakes. Why would I check the green check mark when the problem is not done and does not compile
@cblack618
"Dude", "Pump your brakes" - what sort of monkey talk is that?

Why would I check the green check mark when the problem is not done and does not compile
Well, given your intelligence deficit and your rung-status on the simian branch of evolution, it would be wasted spelling out the reasons to you. Humans know that the problem is solved.
Go troll someplace else
Pages: 123