Sorry new to learning C. So I want to program a function that checks, whether the given string is a Palindrome or not. The function itself works, but only if I declare the given string in the code. When it comes from an input-stream it doesn't work, can you give me some help?
The function has a return type, I need that for later, not important now.
Greetings from Germany
I know scanf() isn't recommended for strings, but I tried using fgets() and scanf() and neither of those worked.
Here is my source code.
#include <stdio.h>
int isPalindrome(char* string) {
char *end, *front;
end = string;
while (*end != '\0') {
++end;
}
--end;
while (end >= front) {
if (*end == *front) {
--end;
front++;
}
else {
break;
}
}
if (front > end) {
printf("string is palindrome\n");
return 1;
}
else {
printf("string isnt palindrome\n");
return 0;
}
}
int main() {
char input[1000];
char input2[1000] = "anna";
printf("Enter a string:\n");
//scanf("%999s", input);
fgets(input, 1000, stdin);
printf("%s\n", input);
isPalindrome(input);
isPalindrome(input2);
}