Here's something I managed to whip up thanks to your logic--
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::flush;
using std::string;
using std::vector;
bool isPalindrome(const string&);
const char* result(bool);
int main(){
vector<string> values;
values.push_back("mom");
values.push_back("ffaff");
values.push_back("dad");
values.push_back("nonPalindrome");
values.push_back("blah");
values.push_back("A");
values.push_back("");
for(vector<string>::iterator i = values.begin(); i != values.end(); i++)
cout << (*i) << " is a Palindrome? - " << result(isPalindrome((*i))) << endl;
cout << "\n\nEnter a word to be determined as a palindrome" << endl;
values[0].clear();
cin >> values[0];
cout << values[0] << " is a Palindrome? - " << result(isPalindrome(values[0])) << endl;
cin.ignore();
cin.get();
return 0;
}
/**
* Returns true if the argument string is a palindrome, returns false otherwise
*/
bool isPalindrome(const string& arg){
/*
* LOGIC:
* -If string length is 0 or 1
* -string is a palindrome, so return true
* -Else
* -If first and last indice of said string have the same char
* -generate a new string that is a substring of this string
* discluding the first and last indice of the previous string
* -Else
* -return false
*/
string temp (arg);
if(temp.length() == 0 || temp.length() == 1)
return true;
else{
if(temp[0] == temp[temp.length() - 1]){
string next = temp.substr(1, temp.length() - 2); // substring from indice 1 and up to (length() - 2) from 1
// i.e. - ffaff, f is at [1] and length is 5, so from 1 go up to 2+1
// because even though length() - 2 is 3, we go up to 3 but not at 3
// so only 2 is added
return isPalindrome(next); // checking to see if the parsed string is a palindrome or not
}else return false;
}
}
/**
* Convenience method for returning a stringified version of a boolean argument.
*/
const char* result(bool arg){
return arg ? "TRUE" : "FALSE";
}
--though it's probably not nearly as efficient as your implementation.
What you may want to consider is recursively calling your function by pre-incrementing index and pre-decrementing the length so you can simply check the characters of a string based on the parameters of your method and not have to mess around with arrays much.
It's just a suggestion, but I think you'll find things much easier that way =)