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
|
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
using std::endl;
using std::cout;
using std::cin;
bool Palindrome(int first, int last, char inputString[]);
char revers_string();
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
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;
if (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;
}
else(choice == 1);
{
string inputString;
cout << "\n\nPlease enter a word or phrase: ";
cin >> inputString;
string reverse_string(inputString.rbegin(), inputString.rend());
cout << "\nReverse string is : " << reverse_string << endl;
//here is where the reverse string part should go.
}
}
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)
return true;
//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])
return false;
//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.
else if (inputString[first] == inputString[last])
{
return Palindrome((first+1),(last-1),inputString);
}
}
|