Help with Palindrome Project

Hello All,

I have recently started my education path for getting into the cybersecurity field. I am currently in a C++ course and learning the ins and outs. I look forward to any and all input that is presented and I hope to learn as much as possible. I have looked through the forums and other websites as well to help me assemble what I have so far. Now I have hit a bit of a snag.

-My program should read in a string and determine if it is a palindrome, case of the letter should not make a difference and the program should end when the user enters "END". Here is what I have so far when I attempt a build it tells me that it should be (int main) and not (void main) can anyone explain why? Also, i have not figured out a way to work in ignoring the letter case without crashing my program. Thank you in advance for any and all assistance.

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
  
#include <iostream>
using namespace std;

int main(){
    char string1[20];
    int i, length;
    int flag = 0;
    
    cout << "Enter a string: "; cin >> string1;
    
    length = strlen(string1);
    
    for(i=0;i < length ;i++){
        if(string1[i] != string1[length-i-1]){
            flag = 1;
            break;
   }
}
    
    if (flag) {
        cout << string1 << " is not a palindrome" << endl; 
    }    
    else {
        cout << string1 << " is a palindrome" << endl; 
    }
    system("pause");
    return 0;
}
it tells me that it should be (int main) and not (void main) can anyone explain why?

Because that's what the Standardâ„¢ says: main must return int. If you're following a tutorial that uses void main or something, then you're following an outdated tutorial, and should frankly burn it. main is a special function that should just be seen as the starting point of the program, and return int (to make matters more confusing, the actual return statement is optional).

As for the rest, strlen function is defined in <cstring> header, so you need to include that.
To ignore case, either change everything to toupper, or change everything to tolower.

I also suggest using std::strings, since you are clearly using C++ and not C.
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
#include <iostream>
#include <string> // std::string, note: different than <cstring>, no need to use strlen function
#include <cctype> // toupper

int main() {
    using namespace std;

    std::string string1;
    bool flag = false;
    
    cout << "Enter a string: "; cin >> string1;
    
    int length = string1.length();
    
    for (int i = 0; i < length; i++) {
        if(toupper(string1[i]) != toupper(string1[length-i-1])){
            flag = true;
            break;
        }
    }
    
    if (flag) {
        cout << string1 << " is not a palindrome" << endl; 
    }    
    else {
        cout << string1 << " is a palindrome" << endl; 
    }
    
    return 0;
}
Last edited on
Ganado,

Thank you for all of that information. I used the void only because that is what the professor told us to use in the beginning. I will give this a shot and put it inside a do while loop to account for the "END" stop requirement. Again thank you for all of the help.
Topic archived. No new replies allowed.