It worked!! Thank you.
so now I need to work on the second part which is:
I need to ask the user if they want to check if the string is a palindrome or a reverse string.
1. I need to create a reverse_string() recursive function to take that same user input and print it out in reverse.
2.output should look like this as examples:
"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 1 (example input)
Enter your string: Hello World (example input)
reversed string is: dlroW olleH
"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 2
Enter your string: madam
madam is a palindrome
"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 2
Enter your string: able
able is not a palindrome.
Here is what I ave done so far:
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include<iostream>
#include<string>
using namespace std;
string remove_char(string);
bool is_palindrome(string&, int, int);
string reverse();
int main()
{
string userInput;
string sentence2;
int first =0;
size_t last = string::npos;
string answer = ‘yes’;
int userchoice;
while (answer == ‘yes’)
cout<< "Do you want to reverse a string(1), determine if it is a palindrome(2) or quit:"<<endl;
cin>>userchoice<< endl;
if ((userchoice !=- “1”) && ( userchoice != “2”))
{
cout>>“Game over!”>>endl;
answer = “no”;
return 0;
}
Cout<< “Please enter your text:”<< endl;
getline (cin, userInput);
if (userchoice =”1”)
{
/*call reverse function here*/
}
Else
{
/*Palindrome function here */
}
Cout<< “Would you like to try again, yes or no?” <<endl;
Cin>> answer;
}
sentence2 = remove_char(userInput);
cout<< (is_palindrome(sentence2, first, last) ? " is a palindrome.":" is not a palindrome.") <<endl;
cin.get();
return 0;
}
string remove_char(string userInput)
{
string sentence2;
for (unsigned int x=0; x < userInput.length(); x++)
{
if (isalnum(userInput[x]))
{
sentence2 += userInput[x];
}
}
return sentence2;
}
bool is_palindrome(string& sentence2, int first = 0, int last = string::npos)
{
if (last == string::npos)
{
last = (sentence2.length()-1);
}
if (sentence2[first]== sentence2[last])
{
if ((first-last)==0)
{
return true;
}
else if (first ==(last-1))
{
return true;
}
else
{
return is_palindrome(sentence2, first+1, last-1);
}
}
else
{
return false;
}
}
String reverse()
{
//need to figure this part out
}
|
Edit: there are a coupleof errors ie Cout instead of cout. I am aware of those.
I need to use the toupper() or tolower() functions to change characters to whichever case you want. Somehow I have to incorporate these to ingore the case sensitive problem.