【问题标题】:What is the best way to compare multiple strings in C? [closed]在 C 中比较多个字符串的最佳方法是什么? [关闭]
【发布时间】:2019-04-28 12:58:34
【问题描述】:

我正在尝试找到在 C 中比较多个字符串的最佳方法。 目前,我正在使用strcmp(); 函数,但结果是太多了if 陈述。我也在使用三元运算符,但不幸的是,它没有帮助。 有没有更好的解决方案? 这是示例代码:

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

int main()
{
    char command[] = "First Second Third";
    char * del = " ";
    char * token;
    char * strings[3];
    int n = 0;
    token = strtok(command, del);

    while (token != NULL){

        strings[n] = token;
        token = strtok(NULL, del);
        n++;
    }
    // Now, strings[0] = "First", strings[1] = "Second", and strings[2] = "Third"

    // Only examine strings[1] and strings[2] after we know strings[0] = "First".
    if (strcmp("First", strings[0]) == 0){
        //do something
        if (strcmp("Second", strings[1]) == 0){
            //do something
            if (strcmp("Third", strings[2]) == 0){
                //do something
                printf("CORRECT");
                //do something
            }
        }
    }

    return 0;
}

【问题讨论】:

  • 向我们展示您的代码。
  • 请通过展示您的实际问题或您想要优化的具体案例的代码来集中您的问题。
  • 有时您只需要输入大量代码 - 不要寻找掩盖您正在尝试做的事情的技巧
  • 这听起来像是一个 X/Y 问题 - 为什么你想匹配多个字符串?你想通过匹配多个字符串来完成什么?您正在尝试做什么的高级描述是什么?

标签: c string strcmp


【解决方案1】:

看起来您的if 语句应该使用strcmp() 而不是发布的strtok()。另外,我认为您正在寻找更像if ... else if ... else if ... 的东西,而不是嵌套的if's

根据您正在编写的实际程序(与发布的程序相比),您还可以使用基于字符串中第一个字符的switch() 进行比较。它相当于一个粗略的散列函数。 (您可能想了解有关散列的更多信息,以使您拥有的东西适应您想要的东西)。


编辑: 也许这就是你得到的: . . .

    if (strcmp(strings[0], "cmdA") == 0)
        processCmdA(strings[1], strings[2]);
    else
    if (strcmp(strings[0], "cmdB") == 0)
        processCmdB(strings[1], strings[2]);
    else
    if (strcmp(strings[0], "cmdC") == 0)
        processCmdC(strings[1], strings[2]);
.
.
.
void processCmdA(char* optionA, char* inputFileName)
{
    if (strcmp(optionA, "parse") == 0)
        parseFile(inputFileName);
    else
    if (strcmp(optionA, "cksum") == 0)
        computeCheckSum(inputFileName);
    else
.
.
.
}

void parseFile(char* inputFileName)
{
    // do parse stuff
}
.
.
.

【讨论】:

  • 对于if 语句中strtok 函数的错误,我们深表歉意。我编辑了帖子。我想要做的是检查命令是否正确。它的各个部分可能会有所不同,例如 char command[] = "First Second Third";char command[] = "foo bar";
【解决方案2】:

OP的代码有一些问题

  • while (token != NULL) 没有限制为 3 个循环。代码可能会尝试strings[3] = token;

    // while (token != NULL){
    while (n < 3 && token != NULL){
    
  • 代码使用strings[0]strings[1]strings[2],而没有首先确保解析了许多令牌。

    // strcmp("First", strings[0]) == 0
    (n > 0 && strcmp("First", strings[0]) == 0)
    
  • 代码保存指向原始字符串的指针。一旦再次调用strtok(),之前的令牌可能会丢失/更改。


“最佳”方法涉及对键和目标进行散列处理,但这里要解释的内容很多。

替代方案:在 OP 的示例中需要这样一个简单的匹配,代码可以使用"%n" 来记录扫描的偏移量。

int n1 = 0, n2 = 0, n3 = 0;
sscanf(command, "First %n Second %n Third %n", &n1, &n2, &n3);

if (n1) {      // n1 is non-zero if scan matched "First"
    //do something
    if (n2) {  // n2 is non-zero if scan matched "First Second"
        //do something
        if (n3) {
            //do something
            printf("CORRECT");
            //do something
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多