Google works well to find palindrome programs for numbers, but the ones I find for words generally don't use a function, which is a must for my program. I've tried writing one but after spending a lot of hours on it I still can't get it to compile, so I was hoping for some help. The logic I used is simple...Prompt user to input word, put that word into a char array, call function which creates a second array which is initialized to the reverse of the first array. Then compare each letter in the arrays, and if they're the same increase a counter. Finally, return the counter to main and if the counter == the length of the array, then it's a palindrome. My code is:
#include<iostream>
#include<string>
#include<cstring>
usingnamespace std;
int pcheck(char word[], char rword[], constint SLEN);
int main(){
char word[100];
int p;
cout << "Which word would you like to check to see if it's a palindrome?" << endl;
cin.getline(word, 99);
constint SLEN=strlen(word);
char rword[SLEN];
for(int i=0, i<SLEN, i++){
rword[i]=word[SLEN-i];
}
int p=pcheck(word, rword, SLEN);
if(p==SLEN){
cout << "It is a palindrome!" << endl;
}
else{
cout << "It is not a palindrome." << endl;
}
}
int pcheck(char word, char rword, constint SLEN){
int j=0, partn=0;
while(j<SLEN){
if(word[j]==rword[j]{
partn++;
}
else{
cout << "Not a palindrome" << endl;
}
j++
}
I can't figure out why it won't compile. Among other errors, it tells me I'm trying to declare arrays with non-constant integers.
Compilation errors/warnings:
line 16: Error: Multiple declaration for i.
line 16: Error: "," expected instead of "<".
line 16: Error: Use ";" to terminate declarations.
line 16: Error: Expected an expression.
line 19: Error: Multiple declaration for p.
line 20: Warning: The variable p has not yet been assigned a value.
line 30: Error: Pointer type needed instead of char.
line 30: Error: Pointer type needed instead of char.
line 30: Error: ")" expected instead of "{".
line 37: Error: Use ";" to terminate statements.
line 40: Error: A declaration was expected instead of "}".
I changed my code to just initialize char rword[100] to avoid the issue of having a non constant there, and the above are my current errors.
Would physically determining the length of the input solve my error?