#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
usingnamespace std;
using std::endl;
using std::cout;
using std::cin;
bool Palindrome(int first, int last, char inputString[]);
void reverse(string inputString);
int main()
{
int choice;
int first = 0;// first index for array
int last = 0;// last index for array
char inputString[100];//initialize size of array
bool running = true;
do{
cout << " " << endl;
cout << "Do you want to reverse a string, check if it a palindrome, or quit?" << endl;
cout << "(Press 1 for reverse, 2 for palindrome, and anything else to quit)" << endl;
cin >> choice;
switch(choice)
{
case 1:(choice == 1);{
string inputString;
cout << "\n\nPlease enter a word or phrase: ";
cin.ignore();
getline(cin, inputString);
reverse(inputString);
break;
}
case 2:(choice == 2);{
cout << "\n\nPlease enter a word or phrase: ";
cin >> inputString;//receive input from user
last = strlen(inputString);//store input into character array
if (Palindrome(first,last - 1,inputString)== true)
cout << "You have entered a palindrome!!!";
else
cout << "This is not a palindrome.";
cout << " " << endl;
//return 0;
break;
}
default:{
cout << "Thank you and have a good day!" << endl;
running = false;
break;
}
}
}while(running);
///here is wher the reverse string part should go.
return 0;
}
bool Palindrome(int first, int last, char *inputString){
//if the next first index is greater than the next last index
//program concludes that all the elements in array have been compared
//and therefore input should be a palindrome retuned as true.
if (first > last)
returntrue;
//if next first element and next last element are not the same
//therefore word is not a palindrome returned as false
if (inputString[first]!= inputString[last])
returnfalse;
//if next first element and next last element are same
//call Palindrome function again, increment first element by one
//decrement last element by one to compare if those two elements
//in array are equal.Continue this process until next first element
//and next element are not equal.
elseif (inputString[first] == inputString[last])
{
return Palindrome((first+1),(last-1),inputString);
}
}
void reverse(string inputString){
string reverse_string(inputString.rbegin(), inputString.rend());
cout << "\nReverse string is : " << reverse_string << endl;
}
So this code compiles and runs great until I select "2" and input "mom" and it tells me its a palindrome just fine. Now if I enter "mom dad" the program freaks out and just keeps repeating itself forever until I force stop the function. Now if I use the return 0; that is commented out in the palindrome case #2 it stops when it is a palindrome with 2 or more words. But it stops the program and does not do the do while loop. Why does this program freak out?
Line 47: cin >> inputString; can get only one word. In your case it leaves " dad" in input buffer. Next it tries to get int choice, finds 'd' symbol, which isn't a digit and cannot be converted to int, sets failbit, and starts over, finds 'd' char...
"The problem I am having is creating a new recursive function that I will call reverse_string() that takes a string of characters and reverses it."
And as it follows you meant a character array when you were saying about a string.
So the code you have now does not satisfies your first requirements. First of all the function is not recursive and secondly it does not use a character array.
yes i do need that and when I tried this it gave me this repeating:
YPuTTYollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1oll1ÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿ