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.
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.
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.
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 ) returntrue;. When the computer reach that line i=0 and j=0.
What is the purpose of the for loop?
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 [)
#include <string>
#include <iostream>
usingnamespace 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
}
elseif (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;
}
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?