Need help.. strcmp code

Hello,

I am trying to compare two strings using the strcmp operator. The program is to prompt me if the word or number I typed in is a palindromes. Palindromes reads the same forwards as backwards.

For example:

deed is a palindrome
civic is a palindrome

Please see my code below.


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

#include <iostream>
#include <cstring>

 using namespace std;

 int main()
 {
    char typed[51];
    char reverse[51] ;
    int length = 0;
    int count = 0;
    int i = 0;
    

    cout << "Please type a word or number with only 50 characters:" << endl;
    cin >> typed;
    length = strlen(typed);

  

    for(count = length - 1;count >= 0; count--)
    {
        reverse[i] = typed[count];
        i++;
    }

        

        if(strcmp(typed,reverse) == 0)
        {
        cout << "The input word or number is a palindrome." << endl;
        }
        else
        {
        cout << "The input word or number is NOT a palindrome." << endl;
        }

    return 0;
 }


When I type in civic I get false and it would prompt me the else statement.

Last edited on
You forgot to mark the end of the reversed string with a null character '\0'.
Thanks Peter.

Ok. I added the null character to my code. Not sure if it is the right way. Below is my updated code. Am I doing it wrong?

When I type in "civic" my reverse output is showing me civic@.

I used the web compiler today and tested my code. It seem to work fine. For example. I typed in civic and I checked my cout for reverse and it showed me civic without the "@" at the end.

When I compile my code in C-Block(exactly the same code as what I have on the web complier) it gives me a different result (civic with the "@" symbol). Does this mean anything?

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
#include <iostream>
#include <cstring>

using namespace std;

 int main()
 {
    char typed[51];
    char reverse[51];
    int length = 0;
    int count = 0;
    int i = 0;
   
    //prompt the user to type in a word or number
    cout << "Please type a word or number with only 50 characters: ";
    cin >> typed;
    length = strlen(typed);

  
    //spell the work backward and place in reverse
    for(count = length - 1;count >= 0; count--)
    {
        reverse[i] = typed[count];
        i++;
    }

    //validate to see if there are any blank spaces or null
    while (reverse [i] != ' ' && reverse[i] != '\0')
    {
        i++;
    } 
    
  
    // to replace the blank space with null
    if (reverse[i] == ' ')
    {
        reverse[i] = '\0';
    }
    
    
        //test to see if the word or number typed in is a palindrome
        if(strcmp(typed,reverse) == 0)
        {
        cout << "The input word or number is a palindrome." << endl;
        }
        else
        {
        cout << "The input word or number is NOT a palindrome." << endl;
        }

    return 0;
 }
Last edited on
It's still not correct.

When you define an array like this char reverse[51]; all 51 characters in it will be uninitialized so you can't be sure what they will be.

Later you assign to the first characters in the array, that's good so far, but the reason why your first program failed was because strcmp expects the strings to be terminated using a null character, that's the only way it could know where the string ends. You could be lucky and get a null character at the correct position, so it might work sometimes, and it might always work on some compilers with some compiler settings. It's nothing to rely on anyway.

To make sure the reversed string ends with a null character you could make sure all characters are initialized to null characters before writing the other strings data to it. You can do that by adding an extra pair of curly brackets at the end of the array declaration.
 
char reverse[51]{}; // all characters in reverse is initialized to '\0' 

Or you could just write a null character after you have copied the string data. You know that the length of the reversed string is supposed to be the same as the other string so you could use the length variable to write the null character at the correct position.
 
reverse[length] = '\0'; // marks the end of the reversed string 
Last edited on
Thanks for your help Peter! Really appreciated!
Topic archived. No new replies allowed.