C++ program to detect palindromic words

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

int pcheck(char word[], char rword[], const int 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);
 const int 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, const int 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.
Last edited on
When you get errors, just copy and paste them here, don't try to summarize them.

1
2
const int SLEN=strlen(word);
char rword[SLEN];


strlen() is a function, so it's value is determined at run time. const values however, must have their values set at compile time.
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?
Last edited on
Topic archived. No new replies allowed.