0

I'm coming from Python and there you can easily compare two strings (char arrays) like that:

if "foo" == "bar":
    # ...

How would I do this in C? I already saw this post but it didn't work.

Code:

int main(void) {

    char string[100];
    
    printf("Please enter something: ");
    fgets(string, 100, stdin);

    if (strcmp(string, "a") == 0)
        printf("a");

    else if (strcmp(string, "b") == 0)
        printf("b");

    else if (strcmp(string, "c") == 0)
        printf("c");

    else
        printf("something else");


    return (0);
}

It prints "something else" though I entered a.

puncher
  • 1,570
  • 4
  • 15
  • 38

3 Answers3

2

The function fgets can append to the entered string the new line character '\n' that you should remove. For example

fgets(string, 100, stdin);
string[ strcspn( string, "\n" ) ] = '\0';

or

fgets(string, 100, stdin);
char *p = strchr( string, '\n' );
if ( p != NULL ) *p = '\0';

From the C Standard (7.21.7.2 The fgets function)

2 The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The fgets() also takes in the newline character when the user presses the Enter key. So in order to remove that from what is stored in `string[100], just change :

fgets(string, 100, stdin);

to

strtok(fgets(string, 100, stdin), "\n");

to remove the newline character. Just make sure to include the #include <string.h> header

Further Reading

If you are wondering what strtok does, have a look at https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

The C library function char *strtok(char *str, const char *delim) breaks string str into a series of tokens using the delimiter delim.

char *strtok(char *str, const char *delim)

Criss Hills
  • 189
  • 1
  • 12
0

You could answer this question yourself if you did put some research effort.

int main(void) {

    char string[100];
    
    printf("Please enter something: ");
    fgets(string, 100, stdin);

    if (strcmp(string, "a") == 0)
        printf("a");

    else if (strcmp(string, "b") == 0)
        printf("b");

    else if (strcmp(string, "c") == 0)
        printf("c");

    else
    {
        printf("something else - lets check why:\n Entered string:\n");
        for(size_t n = 0; n <= strlen(string); n++)
        {
            printf("string[%2zu] = ", n);
            switch(string[n])
            {
                case '\n':
                    printf("\\n");
                    break;
                case '\r':
                    printf("\\r");
                    break;
                case 0:
                    printf("null char");
                    break;
                default:
                    if(string[n] < ' ' || string[n] > 127)
                    {
                        printf("\\x%x", (unsigned)string[n]);
                    }
                    else
                    printf("'%c'", string[n]);
            }
            printf("\n");
        }
    }


    return (0);
}

https://godbolt.org/z/afb3Ej7E9

0___________
  • 60,014
  • 4
  • 34
  • 74