Okay so I've been working on it and broke it all down and got this which works beautifully. My next step was to add a do-while like:
Code:
do{
printf("Enter what you think is a palindrome (w/o spaces): ");
fgets(string, sizeof(string), stdin);
}while(string =! "\n");
so that it will continue to prompt the user unless they hit enter. The problem is "string" is a character array and \n I thought would be considered a character but it must be reading it as an int because my error message is that they are "incompatible types". I don't understand how to change this. Insight?
Code:
#include<stdio.h>
#include<string.h>
int main(void) {
char string[100];
int i, len, pal;
printf("Enter what you think is a palindrome (w/o spaces): ");
fgets(string, sizeof(string), stdin);
len = strlen(string)-1;
for(i=0; i<=len/2; i++)
if(string[i] == string[len-1-i]) {
pal = 0;
}
else {
pal = 1;
}
if(pal == 0) {
printf("Yes it is a palindrome! \n");
}
else if(pal == 1) {
printf("Not a palindrome");
}
else {
printf("Something is wrong");
}
}
The next step is where I am getting stuck. To finish the program I wanted to put palindrome into a function like below:
Code:
#include<stdio.h>
#include<string.h>
int palindrome(char string);
int main(void) {
char string[100];
printf("Enter what you think is a palindrome (w/o spaces): ");
fgets(string, sizeof(string), stdin);
if(palindrome(string) == 0) {
printf("Yes it is a palindrome! \n");
}
else if(palindrome(string) == 1) {
printf("Not a palindrome");
}
else {
printf("Something is wrong");
}
}
int palindrome(char string) {
int i, len, pal;
len = strlen(string)-1;
for(i=0; i<=len/2; i++)
if(string[i] == string[len-1-i]) {
pal = 0;
}
else {
pal = 1;
}
return(pal);
}
but am getting errors that palindrome makes integer from pointer w/o cast. Does my declaration of palindrome need to be char* string to say that string points to a string?