Stuck on making palindrome work for a c string

I am trying to implement and use the palindrome code that my instructor provided but it seems to output "is not a palindrome" for every output

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>

using namespace std;

//List function prototypes below
int lastIndexOf(const char*, char);
void reverseCString(char*);
bool isPalindrome(const char*);
void toUpper(char*);
int numLetters(const char*);

int main()
{
    int menuChoice;
    char letter, response;
    char str1[] = "0123456789a1A";
    char str2[] = "Reverse";
    char str3[] = "Flower";
    char str4[] = "wertytre";
    char str5[] = "radar";
    char str6[] = "abc ba";
    char str7[] = "ra ar";
    char str8[] = "abccBa";
    char str9[] = "abcdefgha";


    do{

	//Interface that pops up when the user runs the program so he can choose an option
    cout << "---------------------------------------------------------------------" << endl;
    cout << "           C - STYLE PROCESSING WITH NO STRING OBJECT!" << endl;
    cout << "---------------------------------------------------------------------" << endl;
    cout << "1. Finding the last index of a number or integer (Entered by user)" << endl;
    cout << "2. Reverse char array" << endl;
    cout << "3. Detecting the number of replacement" << endl;
    cout << "4. Determine if the string is a palindrome" << endl;
    cout << "5. Convert a user Defined string to uppercase" << endl;
    cout << "6. Display the amount of letters in an array" << endl;
    cout << "7. Exit program" << endl;

    cout << endl;

    //Prompts user to select an option
    cout << "Enter your choice: ";
    cin >> menuChoice;

        // a sample validation routine to try catch input error
        while (!cin || menuChoice > 10 || menuChoice < 1)
        {
            cout<<"INVALID CHOICE ...please retype"<<endl;
            cin.clear();
            cin.ignore();
            cin >> menuChoice;
        }

    //Interface and function calls with arguments that do the process for every option
    switch(menuChoice)
    {
        case 1: {   cout << endl;
                cout << "Enter a letter to know the last index of in the string " << str1 << ": ";
                cin >> letter;
            int index = lastIndexOf(str1, letter);
            if (index == -1 & index != toupper(letter))
                cout << "The last index in the cstring " << str1 << " with the character '" << letter << "' is " << "-1" << endl;
            else
                cout << "The last index in the cstring " << str1 << " with the character '" << letter << "' is " << index << endl;
                cout << endl;
                }
            break;
        case 2:
            cout << endl;
            cout << "The original string is " << str2 << "." << endl;
            reverseCString(str2);
            cout << "The reversed string is " << str2 << endl;
            cout << endl;
            cout << "The original string is " << str3 << "." << endl;
            reverseCString(str3);
            cout << "The reversed string is " << str3 << endl;
            cout << endl;
            break;
        case 3:
            break;
        case 4:{
            if(isPalindrome(str4))
            {
                cout << "The string is a palindrome" << endl;
            }
            else
            {
                cout << "The string is not a palindrome" << endl;
            }
            if(isPalindrome(str5))
            {
                cout << "The string is a palindrome" << endl;
            }
            else
            {
                cout << "The string is not a palindrome" << endl;
            }
                }
            break;
        case 5: 
            break;
        case 6:  cout << endl;
            cout << str1 << " has this many letters: " << numLetters(str1) << endl;
            cout << endl;
            break;
        case 7:cout<<endl;
                cout<<"Array processing test now concluded. Exiting program ....."<<endl;   //Input validation to exit program
            break;
        default:  cout<<"INVALID CHOICE ...please retype"<<endl;    //Input validation when the user enters an integer not within the range of 1-10
            break;
    }


    //Prompts user if they would like to run the program again
    cout << "Type Y to run the program again or any other key to exit: ";
    cin >> response;

    cout << endl;

    //printLetters(strHolder);
    }while(response == 'Y' || response == 'y');

    cout << "Now exiting the c-style processing program ....." << endl; //Terminating message

    return 0;
}

int lastIndexOf(const char* inString, char target)
{
    int index = -1;

       for (int i = 0; i < strlen(inString); i++)
            if (inString[i] == target)
                index = i;
                return index;
}

void reverseCString(char *inString)
{
    int i = 0,j = strlen(inString) - 1;
    int temp;

    while(i < j)
    {
        temp = inString[i];
        inString[i] = inString[j];
        inString[j] = temp;
        i++;
        j--;
    }

}

bool isPalindrome(const char* inString)
{
    int startIndex = 0, endIndex = strlen(inString) + 1;

    while(startIndex < endIndex)
    {
        if(!inString[startIndex] == inString[endIndex] == false)
        return false;
        startIndex++;
        endIndex--;
    }

    return true;
}

/*void toUpper(char* inString)
{
        if(inString == NULL) return;
        if(inString[0] >= 'a' && inString[0] <= 'z') inString[0] += (char)('A' - 'a');
} */

int numLetters(const char* inString)
{
    int letterCount = 0;

    while(*inString != '\0')
    {
        if(isalpha(*inString))
            letterCount++;
            inString++;
    }
    return letterCount;
}
implement and use the palindrome code that my instructor provided

What, exactly, was provided to you?

Did you write the:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isPalindrome(const char* inString)
{
    int startIndex = 0, endIndex = strlen(inString) + 1;

    while(startIndex < endIndex)
    {
        if(!inString[startIndex] == inString[endIndex] == false)
        return false;
        startIndex++;
        endIndex--;
    }

    return true;
}

That was provided for me the bool code
the bool code

Why I have a feeling that you don't mean the entire function?

Lets take a simple string: "X".
That has one character. The C-string is an array of two characters: {'X', 0}
The strlen() of that C-string is 1. Therefore, endIndex == 2

The valid indices of that array are 0 and 1. 2 is not valid. It is an out-of-range error.


How do you evaluate ! aa == bb == false?
Which operator has higher priority, the not (!) or the equal (==)? In other words, is
1
2
3
4
5
! aa == bb
// same as
(! aa) == bb
// or same as
! (aa == bb)

What is associativity of ==, is
1
2
3
4
5
aa == bb == cc
// same as
(aa == bb) == cc
// or same as
aa == (bb == cc)


Did your instructor really give you code full of logical errors and bits that require deep knowledge of operators?
Yeah, it was apart of one of the programs he told us to look at for reference, I came up with this right now.


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

using namespace std;

bool isPalindrome(const char*);

int main()
{
    char lmao[] = "radar";
    char test[] = "random";
    char test2[] = "madam";

    if(isPalindrome(test))
        cout << "The string is a palindrome";
    else
        cout << "The string is not a palindrome";


    return 0;
}

bool isPalindrome(const char* str)
{
    //Initializes variables
    int i = 0, j = strlen(str) -1;

   
     for(i = 0; i < j; i++)
    {
        if(str[i] != str[j])
        {
            return false;
        }
        j--;
    }
}
Last edited on
Topic archived. No new replies allowed.