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.
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.
#include <iostream>
#include <string> // std::string, note: different than <cstring>, no need to use strlen function
#include <cctype> // toupper
int main() {
usingnamespace 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;
}
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.