简体   繁体   English

如何比较 C 中的两个字符串?

[英]How to compare two strings in C?

I want to compare 2 strings and if they're equil it should print something.我想比较 2 个字符串,如果它们相等,它应该打印一些东西。 I saw a post on this already but it doesn't work at me.我已经看到了一个帖子,但它对我不起作用。 Post: How to compare strings in an "if" statement?帖子: 如何比较“if”语句中的字符串?

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 .尽管我输入a .

The function fgets can append to the entered string the new line character '\n' that you should remove. function fgets可以 append 将您应该删除的新行字符'\n'输入到输入的字符串中。 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)来自 C 标准(7.21.7.2 fgets 函数)

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. 2 fgets function 从 stream 指向的 stream 中读取最多比 n 指定的字符数少 1 到 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.在读入数组的最后一个字符之后立即写入 null 字符。

The fgets() also takes in the newline character when the user presses the Enter key .当用户按下Enter 键时, fgets()也会接收换行符。 So in order to remove that from what is stored in `string[100], just change:因此,为了从 `string[100] 中存储的内容中删除它,只需更改:

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只需确保包含#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如果您想知道strtok的作用,请查看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. C 库 function char *strtok(char *str, const char *delim) 使用分隔符 delim 将字符串 str 分解为一系列标记。

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

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 https://godbolt.org/z/afb3Ej7E9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM