Patrick B. answered 05/26/20
Math and computer tutor/teacher
using namespace std;
// You can put the namespace call before the #include <iostream>.... it doesn't matter!!!
#include <iostream>
#include <string>
#include <string.h>
//Not sure what types of strings you wanted, so here is
// the recursive routine using C-strings that are NULL termianted
bool IsPalindrome ( char * str)
{
int n;
bool boolReturn;
if ((n=strlen(str))>1)
{
if (str[0]==str[n-1])
{
str[n-1]=0;
boolReturn = true && IsPalindrome(str+1);
}
else
{
boolReturn=false;
}
}
else // one character string is it's own palindrome by default
{
boolReturn=true;
}
return(boolReturn);
}
/***************************************************
Here is Andrew's function ...
I rewrote it a little bit so that it has only
one return statement;
---------------------------------------------------------
Functions are OVERLOADED: same function names
and return types, but one takes a C-String
and the other std::string
*******************************************************/
bool IsPalindrome(string s)
{
bool boolReturn;
if (s.length() <= 1)
{
boolReturn = true;
}
else
if (s[0] == s[s.length()-1])
{
boolReturn = true && IsPalindrome(s.substr(1,s.length()-2));
}
else
{
boolReturn=false;
}
return(boolReturn);
}
//Here is the main function that YOU were supposed to write
int main()
{
char str[255];
strcpy(str,"ABBA");
cout << IsPalindrome(str)<<endl; //1
strcpy(str,(char*)"HANNAH");
cout << IsPalindrome(str)<<endl; //1
strcpy(str,(char*)"RADAR");
cout << IsPalindrome(str)<<endl; //1
strcpy(str,(char*)"RAZOR");
cout << IsPalindrome(str)<<endl; //0
strcpy(str,(char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA");
cout << IsPalindrome(str) << endl; //1
strcpy(str,(char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZYZWVUTSRQPOMNLKJIHGFEDCBA");
cout << IsPalindrome(str) << endl; //0 the M and N are out of order
cout << "***********************************************" << endl;
//same output
string Str = "ABBA";
cout << IsPalindrome(Str)<< endl;
Str = "HANNAH";
cout << IsPalindrome(Str)<< endl;
Str = "RADAR";
cout << IsPalindrome(Str)<< endl;
Str = "RAZOR";
cout << IsPalindrome(Str)<< endl;
Str="ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA" ;
cout << IsPalindrome(Str)<< endl;
Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZYZWVUTSRQPOMNLKJIHGFEDCBA";
cout << IsPalindrome(Str)<< endl;
}
Tenzin N.
this code does not run in c++ programming and there is not #include and using namespace std; STILL IT DOES NOT WORK!!!05/26/20