Palindrome problem

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.

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
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 = new char[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 = new char[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; 
      return false;
   }
}
The loop on lines 55 to 58 undoes all the work done by the loop on lines 44 to 51.

If line 73 evaluates to true nothing is returned, resulting in undefined behavior.

If you use new[], you should use delete[].
Last edited on
Topic archived. No new replies allowed.