I am writing a program to check if an input string from the user is a palindrome My program always returns false even if the string is a known palindrome. I think my problem lies in the PalindromeChecker function in that this is what always sets the answer variable in the main function to false. However I am unsure as to why this is always the case. Any help finding my problem would be appreciated.
#include "stdafx.h"
#include <iostream>
#include <String>
using namespace std;
char ReadPalIn(string Pal, int len)//converts the string into a char array
{
char PalIn[100];
if (len > 100)
{
cout << "ERROR: Palindrome possibliity too big" << endl;
system("Pause");
}
else
{
for (int i = 0; i < len; i++)
{
PalIn[i] = Pal[i];
}
}
return *PalIn;
}
bool PalindromeChecker(char Pal[],int start, int end)//checks recursively if a string is a palidrome
{
if (start == end)
{
return true; //since there is only one character
}
else if (Pal[start] != Pal[end])
{
//cout << "hi" << endl;
return false;//since that will be the case that decides when something stops being a palidrome
}
else if (start < end + 1)
{
//cout << "hi" << endl;
return PalindromeChecker(Pal, start++, end--);//since we checked both the first and last characters of the char array for palandrominess. <- word of the year?
}
else
{
return true;//base case is the string is a palindrome
}
}
int main()//test if a word is a palidrome using the functions
{
int lengthOfPal = 0;
string PalInd = "abba";
bool Answer = true;
cout << "Hello what string would you like to check?" << endl;
getline(cin, PalInd);
lengthOfPal = PalInd.length();
cout << "You input is: " << PalInd << endl;
cout << "Its Length is: " << lengthOfPal << endl;
system("Pause");
char PalIn[100] = { ReadPalIn(PalInd, lengthOfPal) };
Answer = PalindromeChecker(PalIn, 0, lengthOfPal);
if (Answer == true)
{
cout << PalInd << ": is a palidrome" << endl;
system("Pause");
}
else if(Answer == false)
{
//cout << "hi" << endl;
cout << PalInd << ": is not a palidrome" << endl;
system("Pause");
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire