Hello. I have this lab due in about 2 hours and i does almost everything it is supposed to do. We had this code working earlier but some parts were not saved and we re-did it. The assignment: From the user's point of view, the program should ask for a line of text that might be a palindrome, and when the user enters the line the program should respond "Yes, this is a palindrome!" or "Sorry, this is not a palindrome."
Your program should contain a function that takes a dynamically allocated C-string as an argument and returns a Boolean indicating if the given C-string is a palindrome. The prototype will look like this:
bool isPalindrome(char *line);
Be sure to test every possibility. Is one letter a palindrome? Is no letters? Does your function handle capital letters, spaces, dashes, exclamation points? What if there are a series of special characters, like "Go dog!!!" ?
For some strange reason, our code takes in other phrase (palindrome or not) and says it is a palindrome. I can't seem to find the problem.
int main()
{
//variables
string input;
int count = 0;
//intro message
cout << "Hello welcome to the program" << endl;
cout << "Please enter a palindrome: ";
getline(cin, input);
char *line = newchar[input.length() + 1];
strcpy (line, input.c_str());
isPalindrome(line);
return 0;
}
/*************************************
* Function: isPalindrome
* Description: Creates a new c string removing whitespace and punctuation
and tells the user if the input is a palindrome.
* Input: dynamic c string
* Output: boolean that returns true or false depending
if the c string is a palindrome
**************************************/
bool isPalindrome(char *line)
{
//variables
bool palindrome = true;
int length = strlen(line);
int count = 0;
//get size of c string
while (line[count] != '\0')
{
count++;
}
//copy line to line2 with no spaces and no punctuation
char *line2 = newchar[count + 1];
int count2 = 0;
for (int i = 0; i < count2; i++)
{
if( (line[i] != ' ') && (ispunct(line[i]) == false))
{
line2[count2] = line[i];
count2++;
}
}
line2[count] = '\0';
//all to upper case
for (int i = 0; i < length; i++)
{
line2[i] = toupper(line2[i]);
}
int(length/2);
//determining if palindrome, comparing elements
if (length > 0)
{
for (int i = 0; i < length; i++)
{
if (line2[i] != line2[length - 1 - i])
palindrome = false;
}
}
//display
if (palindrome == true)
{
cout << "The word is a palindrome" << endl;
}
else
{
cout << "The word is not a palindrome" << endl;
returnfalse;
}
}