0

this is just a simple code i just wrote and i dont understand why it wont give me true when i type mother in the stream. It's logical for me when they compare each other it must be the same or?
this is my simple code:

 char string[20];
 fgets(string,20,stdin);

 printf("%s",string);

 if(strcmp(string,"mother")==0)
 {
   printf("they are the same");
 }
 else
 {
     printf("they are not");
 }

i also tried with

 if(string =="mother")
 \\do something

but it wont just give me true can someone help me

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
NewUser2810
  • 47
  • 1
  • 4

4 Answers4

3
 if(string =="mother")  // won't work

You can't compare C strings directly with ==. Use standard library function strcmp instead

if( 0 == strcmp(string, "mother") )
{
    printf( "equal" );
}

Also you would need to remove \n from fgets, otherwise the string comparison deems to false.

fgets(string,20,stdin);
string[strcspn(string, "\n")] = 0;   // without this, `strcmp` would return false
artm
  • 17,291
  • 6
  • 38
  • 54
2

As others have said, you can't compare strings with ==. In order for your code to work, you need to strip the \n character appended from fgets(). Otherwise you are comparing:

if (strcmp("mother\n", "mother") == 0) {

Which will always be false, unless you remove the \n character.

Since fgets() returns NULL if unsuccessful when reading input from the user, it is also safe to check that, like this:

if (fgets(string, 20, stdin) == NULL) {
    /* Exit program */
}

You can also add some extra error checking for fgets(), such as checking for buffer overflow, which can be checked like this:

slen = strlen(string);
if (slen > 0) {
    if (string[slen-1] == '\n') {
        string[slen-1] = '\0';
    } else {
        /* Exit program */
    }
}

After these considerations, your code can possibly look like this:

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

#define BUFFSIZE 20

int
main(int argc, char *argv[]) {
    char string[BUFFSIZE];
    size_t slen;
    const char *compare = "mother";

    printf("Enter a string: ");
    if (fgets(string, BUFFSIZE, stdin) == NULL) {
        printf("Error reading string into buffer.\n");
        exit(EXIT_FAILURE);
    }

    slen = strlen(string);
    if (slen > 0) {
        if (string[slen-1] == '\n') {
            string[slen-1] = '\0';
        } else {
            printf("Buffer overflow. Exceeded buffer size of %d.\n", BUFFSIZE);
            exit(EXIT_FAILURE);
        }
    } 

    if (!*string) {
        printf("No string entered.\n");
        exit(EXIT_FAILURE);
    }

    printf("My string = %s.\n", string);
    if (strcmp(string, compare) == 0) {
        printf("They are the same.\n");
    } else {
        printf("They are not the same.\n");
    }

    return 0;
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
1

fgets does not remove the new-line (\n) from the input. You should remove it (if present) before comparing the strings:

char string[20];
fgets(string,20,stdin);

int len = strlen(string);
// len-1 is the last character before the original \0
if(len > 0 && string[len-1] == '\n'){
    // remove the new-line by ending the string sooner
    string[len-1] = '\0';
}

if(strcmp(string,"mother")==0)

Your second attempt, comparing two strings using == directly will definitely not work.

Kninnug
  • 7,992
  • 1
  • 30
  • 42
0

From the man page for fgets():

fgets()  reads in at most one less than size characters from stream and
stores them into the buffer pointed to by s.  Reading  stops  after  an
EOF  or a newline.  If a newline is read, it is stored into the buffer.
A terminating null byte ('\0') is stored after the  last  character  in
the buffer.

Have you checked if there is a newline character appended to your input? "mother" is not the same as "mother\n".

user3553031
  • 5,990
  • 1
  • 20
  • 40