在字符串的子集上使用strcmp。

时间:2021-10-24 01:44:42

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'};

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

and

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

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

I want to compare the second word of a with the entire string b. I know the starting index of the second word in a. Is there a way to do this directly using strcmp or does more word on a need to be done first?

我想比较a和b的第二个单词,我知道a的第二个单词的起始索引,是否有办法直接使用strcmp,或者需要先做更多的单词?

4 个解决方案

#1


3  

a and b are char arrays, but they are not strings, because they are not null-terminated.

a和b是char数组,但它们不是字符串,因为它们不是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)来比较。

#2


0  

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字节。

#3


0  

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…调用并不是真正正确的调用。

#4


0  

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

hope this helps

希望这有助于

#1


3  

a and b are char arrays, but they are not strings, because they are not null-terminated.

a和b是char数组,但它们不是字符串,因为它们不是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)来比较。

#2


0  

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字节。

#3


0  

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…调用并不是真正正确的调用。

#4


0  

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

hope this helps

希望这有助于