Comparing Strings help!

I have to write a palindrome program.

The main issue I am running into is, if I input Race Car, it will say it is not a palindrome due to the issue of spacing

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

void makeUpper(char* title, char word[]);
void getInput(char *title, char word[]);
void getSpace(char* word1, char* word2);
void getReverse(char word1[], char word2[]);
void stringCopy(char word1[], char word2[]);
char* find(char* start, char what);
bool palindrome(char *word1, char *word2);

int main()//(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);
    char word1[100], word2[100]; //word2 as test variable
    bool again = true; //test value of characters in loop


    while (again)
    {
        getInput("Please enter a sentence: ", word1);

        if (*word1 == '\0')
        {
           again = false;
        }
        else
        {
            getReverse(word1, word2);
            if (palindrome(word1, word2))
                cout <<endl<< "you have a palindrome!" << endl;
            else
             cout <<endl<< "You do not have a palindrome!" << endl;
        }

    }
    cout << "The end!" << endl;
    system ("PAUSE");
    return 0;
    //return a.exec();
}

void getInput(char *title, char word[10])
{
    cout << title;
    cin.getline(word,10); //enable input to be string
    makeUpper("Your sentence upper case is: ", word);


}

void makeUpper(char* title, char *word)
{
    cout << title;

        for (int i = 0; word[i] != '\0'; i++) //as long as word does not equal null
        {
            word[i]=toupper(word[i]); //initialize each word in sentence to uppercase
        }
    /*while (*word != '\0')
    {
        if (*word >= 'a' && *word <= 'z')
            *word += 32;
        // *word = *word - 32
        word++;
    }*/

        cout << word;


}

void getReverse(char word1[], char word2[]) //obtian reverse of word1 to compare word2
{
    char *end = find(word1, '\0');
    end--;
    while (end != word1)
    {
        *word2 = *end;
        word2++;
        end--;
    }
    *word2 = *word1;
    word2++;
    *word2 = '\0';
}


char* find(char* start, char what)
{
    while (*start != what && *start != '\0')
        start++; //increment start of word
    return *start == what ? start : NULL;
    /*
    if(*start == what)
    return start;
    else
    return NULL;
    */
}


bool palindrome(char *word1, char *word2)
{
    bool same = true;
    while (*word1 != '\0')
    if (*word1 != *word2)
    {
        same = false;
        break;
    }
    else
    {
        word1++;
        word2++;
    }
    return same;
}
closed account (j3Rz8vqX)
An attempt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool palindrome(char *word1, char *word2)
{
    bool same = true;
    cout<<endl;
    while (*word1 != '\0')
    {
        while(*word1==' '){++word1;}//Added
        while(*word2==' '){++word2;}//Added
        if (*word1 != *word2)
        {
            same = false;
            break;
        }
        else
        {
            word1++;
            word2++;
        }

    }
    return same;
}
Thank you so much! I can't believe the solution was so easy

So your input is saying

as long as the address of word 1 is not a null termination character
then if address of word 1 finds a space, pre increment word 1 so it moves the variable 1 spot terminating the space?
closed account (j3Rz8vqX)
Yes, skip all spaces since they are not of importance when checking for palindrome.

Have a wonderful day.
Topic archived. No new replies allowed.