Yes or no

Is this a recursive bool function?

#include <string>
#include <iostream>

using namespace std;

bool Function(std::string str)
{//this function remove punctuation and space.


string String;

for (int i=0, j=0;i<str.length();i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
String.append(1,str[i] + 0x20); //make Lowercase by adding 0x20
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
String.append(1,str[i]);
}

if(i>=j)
return true;
if(str[i] == str[j])
return Function(str.substr(i+1,j-i-1));
else
return false;

return(true);

}
}
int main()
{
string strIn;

cout<<"please enter a word phrase\n";
getline(cin,strIn);

if (Function(strIn))
cout<<"it is a palindrome"<<endl;
else
cout<<"it is not a palindrome"<<endl;
}

yes, but as you have it now it always returns true. Try revising your algorithm.
yea, i figure that out i want to say wtf lol? Can you give me some hint on the problem of my algorithm? Why it will always return true and what is the approach for me to fix it? Originally i have

int len = String.length();//compareison

for (int i=0;i<len/2;i++)
{
if (String[i] != String[len-i-1])
{
return(false);
}
}

return(true);
instead of the last if statement. Which the program works well, but my professor want a recursion function so he ask me to redo.
Last edited on
well for starts look
1
2
if(i>=j)
return true;

When is that never going to return true?
"When is that never going to return true?" what do u mean?
Lol, that sentance had some twisty logic xD
Rephrased: When is that ever going to return false?

EDIT:
Well, i guess wtf's sentance made perfect sense.. It's just confusing because you don't usually see a sentence in a negative standpoint talk about truth. Well, that made more sense in my head.
Last edited on
thanks thumper, anywayz..

first of all i would appreciate if anyone can explain to me why the following code will always make it true? And to be precise i can tell "return Function(str.substr(i+1,j-i-1));" <-- that's the problem.

if(i>=j)
return true;
if(str[i] == str[j])
return Function(str.substr(i+1,j-i-1));
else
return false;

return(true);
[code] Your code goes here [/code]
j starts in 0, and is never touched.
i starts in 0, and may be incremented.

And to be precise i can tell "return Function(str.substr(i+1,j-i-1));" <-- that's the problem.
Sorry, what do you mean? If i and j have valids values the call is legal. But you better avoid passing by copy.

Edit: if( i>=j ) is inside the for loop, so in the first iteration everything ends
Last edited on
Ok, i am still vague on what you are saying by "avoid passing by copy" i know it's legal and yea it's suppose to be in the for loop. I run it and no matter what i type it will turn out as "it is a palindrome" How can i make a alternation for possible false? Because right now i am going true no matter what.
bool Function(std::string str) Every time you make a call to Function an string object is created. You expect to make n/2 functions calls. If your word is long you will be wasting a lot of memory.

bool Function(const std::string &str) Now you are using the same object in all the recursive function calls.
But then there is not a reduction
1
2
3
bool Function(const std::string &str, int begin, int end);
template<typename iter> 
bool Function(iter begin, iter end); //range [) 


You are getting always true because of if( i>=j ) return true;. When the computer reach that line i=0 and j=0.
What is the purpose of the for loop?

Also
1
2
3
4
String.append(1,str[i] + 0x20); //make Lowercase by adding 0x20
String.append(1, str[i]-'A'+'a');
String.append(1, tolower(str[i]) );
String.push_back( tolower(str[i]) );
Last edited on
Ok here's my original code, the code test whether it is a palindrome, and it ignores white space and any punctuation. However, my professor asked for a recursive bool to solve it. Thus asking me to redo. I read the recursive tutorials but i don't get the grasp of it to transform my original code into recursive bool.

Lastly whats
1
2
3
bool Function(const std::string &str, int begin, int end);
template<typename iter> 
bool Function(iter begin, iter end); //range [)  

all about? i mean u put a
"&" infront of str.
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
44
45
46
47
48
49
50
51
52

#include <string>
#include <iostream>

using	namespace std;

bool Function(std::string str)
{//this function remove punctuation and space.
	

	string	String;

	for (int i=0, j=0;i<str.length();i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			String.append(1,str[i] + 0x20);		//make Lowercase by adding 0x20
		}
		else if (str[i] >= 'a' && str[i] <= 'z')
		{
			String.append(1,str[i]);
		}
	}

	

	int	len = String.length();//compareison

	for (int i=0;i<len/2;i++)
	{
		if (String[i] != String[len-i-1])
		{
			return(false);
		}
	}

	return(true);

	
}
int main()
{
	string	strIn;

	cout<<"please enter a word phrase\n";
	getline(cin,strIn);

	if (Function(strIn)) 
		cout<<"it is a palindrome"<<endl;
	else
		cout<<"it is not a palindrome"<<endl;
}

Last edited on
I think it is easier to use stacks. Have the function return the palindrome to your main and check if it is a palindrome in your main.

Here's an example I found of some code that will use stacks to check if a word is a palindrome. I hope you can use it in your recursive bool function to solve your assignment :) I stumbled over the code on http://www.blinkenlights.se/ while reading a guide so I didn't write it myself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

	string word, palindrom;
	stack<char> tecken;
	cout<<"Enter word to check: ";
	cin>>word;
	for(int i=0; i<word.size(); i++)
		tecken.push(word.at(i));
	int antal=tecken.size();
	for(int i=0; i<antal; i++){
		palindrom+=tecken.top();
		tecken.pop();
	}
	cout<<word<<' '<<palindrom<<endl;
	if(word==palindrom)
http://www.cplusplus.com/doc/tutorial/functions2/
bool Function(const std::string &str, int begin, int end);
Arguments passed by reference. In this case it is just to avoid the copy.
So I pass the string and the range where it operates. The other prototype is just more general (not attached to a particular container or type)

About the recursive function
A string is a palindrome if:
_ It is empty (base condition)
_ The first and the last character are the same, and the rest of the string is a palindrome (recursive call) Forget for now the spaces and punctuation

In your first post you've got a for loop. But that loop just iterates one time and the function returns. What is the purpose of that loop?

Topic archived. No new replies allowed.