-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void getSentence(char userSentence[]);
int breakSentence_andCompare(char userSentence[] , char compareSentence[]);

#define MAX_SENTENCE 100

int main()
{
    int len = 0;
    char userSentence[MAX_SENTENCE] = {'o','k',0};
    char compareSentence[MAX_SENTENCE] = {'o',0};
    getSentence(userSentence);
    len = breakSentence_andCompare(userSentence,compareSentence);
}

/*
This function is asking the user to input info.
    input:user input string array - char userSentence[].
        output:none.
*/
void getSentence(char userSentence[])
{
    printf("Hello And Welcome To The Palindrome Cheker Made By xXTH3 SKIRT CH4S3RXx");
    printf("\nPlease Enter A Sentence: ");
    fgets(userSentence,MAX_SENTENCE,stdin);
}

/*
This function takes the input of the user and input it into another string backwards.
    input:user input string array - char userSentence[], backward user input string array - char compareSentence[].
        output:strcmp value.
*/
int breakSentence_andCompare(char userSentence[] , char compareSentence[])
{
    int i = 0;
    int z = 0;
    int len = 0;
    int cmp = 0;
    len = strlen(userSentence);
    len -= 1;
    for (i = len ; i >= 0 ; i--)
    {
        compareSentence[z] = userSentence[i];
        printf("%s",compareSentence);   
        z++;    
    }
    printf("\nuser: %s! compare: %s!",userSentence,compareSentence);
    cmp = strcmp(userSentence,compareSentence);
    printf("\n%d",&cmp);
    return cmp;
}

This program checks if inputted string is palindrome, To simply explain how it works:

  1. It takes user input - String.
  2. It Takes the user string and input is backwards in another string.
  3. It compares the strings.

I have a really strange problem in the function and that's the strcmp return value. For some reason, when both strings have the same characters like ABBA the strcmp will return that the value of one of them is bigger. I'd really love to know what is the problem and how can I fix it.

P.S When I were searching the problem I thought that it might be connected to fact that the user input string might contain \n from the enter key; is that possible?

And please understand that this isn't a whole code. The code is missing the output part.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 6
    `strcmp` does not return wrong values, but `printf("\n%d",&cmp);` does. Please change that to `printf("%d\n", cmp);` Note I also moved the newline from the start to the end, similar to the way a fullstop (period) is used in written English. – Weather Vane Jan 06 '18 at 00:01
  • 3
    The line `printf("\n%d",&cmp);` prints the address of `cmp` and not its value. (Your compiler should probably have warned you for that.) – Jongware Jan 06 '18 at 00:03
  • 2
    May want to lop off the potential trailing `'\n'` from `fgets(userSentence,MAX_SENTENCE,stdin);` with `userSentence[strcspn(userSentence, "\n")] = '\0';` – chux - Reinstate Monica Jan 06 '18 at 00:03
  • You can write `char userSentence[MAX_SENTENCE] = "ok";` instead of `char userSentence[MAX_SENTENCE] = {'o','k',0};`. – Pablo Jan 06 '18 at 00:31
  • 4
    Note it's unnecessary to include "beginner" in your title. Everyone seeing the title "strcmp returns wrong value" knows two things: 1) the poster is a beginner, and 2) strcmp is working just fine. – Lee Daniel Crocker Jan 06 '18 at 00:43
  • And that the most likely cause of the problem is failing to propertly check for and overwrite the trialing newline (e.g. `'\n'`) included by `fgets`... – David C. Rankin Jan 06 '18 at 01:25
  • 2
    Speaking of strcmp working just fine, I've been programming for over 20 years, and have found that the faster I assume the fault is with me, the sooner I find a solution. Implicating a common library certainly redirects your attention and dissuades potential assistants, but is almost certainly wrong. – erik258 Jan 06 '18 at 01:34
  • ok i'll remember that,thanks for the advice – SaKaMoToSaN15 Jan 06 '18 at 01:40

1 Answers1

4

The problem is occurring because of trailing \n (newline) character in user input string.

fgets()

The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte. [EMPHASIS MINE]

So, a newline character makes fgets stop reading, but it is considered a valid character by the function and included in the input string.

Say, user input string is "ABBA". So, after entering the input the content of userSentence will be "ABBA\n".

In your program, you are reversing the input string and comparing it with the original input string.

After reversing, the content of compareSentence will be "\nABBA".

The content of both the strings are not same and the strcmp() will return non zero value as the comparison result.

There are various ways of removing trailing \n (newline) character from fgets() input. Check this.

Since you are a beginner, so one suggestion to you - Don't ignore the compiler warning('s).

For the statement -

printf("\n%d",&cmp);

You must be getting a warning message during compilation of your program.

You should try to identify the cause of warning message('s) and should resolve them.

There are much better ways to find out the whether a string is palindrome or not but since you are a beginner, your start is good. Happy Learning.. :)

H.S.
  • 11,654
  • 2
  • 15
  • 32
  • H.S, Thank you very much,and i didnt get any compilation warning/error except no new line at the end.(I am using GCC compiler) – SaKaMoToSaN15 Jan 06 '18 at 08:53
  • 1
    @SaKaMoToSaN15: Use `gcc` option `-Wall` and gcc should give a warning on statement - `printf("\n%d",&cmp);`. – H.S. Jan 06 '18 at 09:01
  • Ok thanks, and i have tried removing \n but it didnt work that well, im tryig it with: for (j = 0; j <= len ; j++) { if (userSentence[i] == '\n') { userSentence[i] = '\0'; } } – SaKaMoToSaN15 Jan 06 '18 at 09:09
  • @SaKaMoToSaN15: You can simply do - `userSentence[strlen(userSentence)-1] = '\0';`. There are other ways also to remove trailing newline character, check the link I have provided in my answer. – H.S. Jan 06 '18 at 09:18
  • THANKS YOU VERY MUCH – SaKaMoToSaN15 Jan 06 '18 at 09:43