0

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);
    
}

  • 2
    Your problem is probably that `fgets` is leaving the `\n` at the end of the string. – Steve Summit Nov 16 '22 at 13:37
  • 1
    [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – Retired Ninja Nov 16 '22 at 13:38
  • 1
    As a side note, I tried compiling the code, and the `isPalindrome` function isn't working at all for me. It says that every string is a palindrome. You forgot to initialize the `front` pointer. (It's a minor miracle that the code ever works for you.) – Steve Summit Nov 16 '22 at 13:44
  • @SteveSummit For me, on mac the code works fine with a given string. The task was to solve it without #include but that seems to be very hard for my knowledge. – Oliver-René Goltsche Nov 16 '22 at 13:47
  • @Oliver-RenéGoltsche You're very close, though — the comments here should help you. The easy ways to strip the `\n` after calling `fgets` do all involve ``, so another option would be to go back to `scanf("%999s", input);`. – Steve Summit Nov 16 '22 at 13:49
  • 1
    Does this answer your question? [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – mch Nov 16 '22 at 13:54
  • 1
    @Oliver-RenéGoltsche I know what you mean when you say "For me, on mac the code works fine with a given string", but I hope you realize that this is, basically, a meaningless statement! It turns out there's a big difference between saying "this code works for me" and "this code works *for the right reasons*, and so should work everywhere". When you have an uninitialized local variable like `front`, if the program seems to work, it's working only by the purest accident. – Steve Summit Nov 16 '22 at 13:55
  • @SteveSummit Okay so my code is shit, got it. I just began :( Even scanf doesnt work at all, still no palindrome. Even if it is one. Im also not here for big lessons in being a programer. But what did I do wrong with the front variable? – Oliver-René Goltsche Nov 16 '22 at 14:01
  • 1
    @Oliver-RenéGoltsche Es tüt mir leid! I didn't say your code was scheiß! It's got one or two mistakes, which you came here for advice with. The point of my little lecture there was not that your code was scheiß. The point of my little lecture was just this: I mentioned that your code didn't work on my machine, and you replied that it *did* work on your machine. You seemed to be saying, if it works on your machine, it might be okay. But there was also a little implication there that you were right and I was wrong for suggesting that it might be broken. That's all. – Steve Summit Nov 16 '22 at 15:47
  • 1
    The reason your code doesn't work on my machine is that, as I and and another commenter said, the variable `front` is never initialized. That is, where does `front` point right before the first trip through the `while (end >= front)` loop? Somewhere before that loop you need to say `front = string`. – Steve Summit Nov 16 '22 at 15:49

0 Answers0