简体   繁体   English

在字符串的子集上使用strcmp

[英]Using strcmp on a subset of a string

I want to use strcmp to compare the subset of a string with another string. 我想使用strcmp将一个字符串的子集与另一个字符串进行比较。

Say I have: 说我有:

a[] = {'h','e','l','l','o',' ','w','o','r','l',d'};

and

b[] = {'w','o','r','l','d};

I want to compare the second word of a with the entire string b . 我想第二个字比较a与整个字符串b I know the starting index of the second word in a . 我知道第二个字的起始索引a Is there a way to do this directly using strcmp or does more word on a need to be done first? 有没有办法做到这一点直接使用strcmp或做更多的字a首先需要做什么?

a and b are char arrays, but they are not strings, because they are not null-terminated. abchar数组,但它们不是字符串,因为它们不是以null结尾的。

If they were modified to null-terminated like this: 如果将它们修改为以null终止,如下所示:

char a[] = {'h','e','l','l','o',' ','w','o','r','l','d', '\0'};

char b[] = {'w','o','r','l','d', '\0'};

And the index of the second word of a is known like you said, then yes, you can use strcmp(a + 6, b) to compare. 就像您说的那样知道a的第二个单词的索引,然后可以使用strcmp(a + 6, b)进行比较。

if (strcmp((a + index), b) == 0) { ... }

strcmp takes two pointers, so you add the index directly. strcmp需要两个指针,因此您可以直接添加索引。

However, you should add a terminating NULL byte to each string. 但是,您应该在每个字符串中添加一个终止NULL字节。

Assuming those are actually strings rather than character arrays with no string terminator, this is quite easy to do. 假设这些实际上是字符串,而不是没有字符串终止符的字符数组,那么这很容易做到。

You sate you no the index of the w in world so it's a matter of: 您对世界上w的索引没有任何要求,所以这是一个问题:

strcmp (b, a+index)

or: 要么:

strcmp (b, &(a[index]))

depending on which you think reads better (the underlying code should be pretty much the same). 取决于您认为哪种读法更好(底层代码应该几乎相同)。

See, for example, this program: 例如,请参阅此程序:

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

int main (void) {
    char *str1 = "world";
    char *str2 = "hello world";

    for (size_t i = 0; i < strlen (str2); i++)
        printf ("%s on match '%s' with '%s'\n",
            strcmp (str1, str2+i) ? "No " : "Yes",
            str1, str2+i);

    return 0;
}

which outputs: 输出:

No  on match 'world' with 'hello world'
No  on match 'world' with 'ello world'
No  on match 'world' with 'llo world'
No  on match 'world' with 'lo world'
No  on match 'world' with 'o world'
No  on match 'world' with ' world'
Yes on match 'world' with 'world'
No  on match 'world' with 'orld'
No  on match 'world' with 'rld'
No  on match 'world' with 'ld'
No  on match 'world' with 'd'

Whether they're string literals or properly terminated character arrays will make no difference. 不管它们是字符串文字还是正确终止的字符数组都没有区别。 Replacing the two declaration lines with: 将两条声明行替换为:

char str1[] = {'w','o','r','l','d','\0'};
char str2[] = {'h','e','l','l','o',' ','w','o','r','l','d','\0'};

will work equally well. 将同样运作良好。

If they're not properly terminated, the str... calls are not really the right ones to use. 如果正确终止,则str...调用不是真正适合使用的调用。

if (strcmp(&a[6],b) == 0)

希望这可以帮助

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

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