如何使用strchr获取c中的子字符串

时间:2015-12-23 11:05:01

标签: c string memcpy memcmp strchr

我正在尝试获取字符串的一部分。
我有以下代码:

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

char mystring[]="The quick brown fox jumps over the lazy dog";
char word1[]="The";
char * posb,pose;
char * word2;
int b,e,n;

n=memcmp(mystring, word1, sizeof(word1)-1);
if (n==0) printf("the first word is found!\n");
posb=strchr(mystring,' ');   // posb will be a pointer to the first character
b=posb-mystring+1;
printf("posb -> %d\n",posb);
printf("b -> %d\n",b);
pose=strchr(mystring+b+1,' ');   // pose will be a pointer to the second character
printf("calc e\n");
e=pose-sizeof(mystring)+1;
printf("pose -> %s\n",pose);
printf("e -> %d\n",e);
word2 = (char*) malloc (sizeof(char)*(e-b));
memcpy(word2, posb, sizeof(word2));
printf("%s:%i:%i:%i\n",word2, b, e, e-b);
free (word2);

问题在于获得第二个单词并将其存储在word2中。为此我尝试使用strchr来定位空格。但是第二次使用strchr我需要一个偏移来找到第二个空格。我尝试了以下方法:

pose=strchr(mystring+b+1,' ');
pose=strchr(&mystring[b+1],' ');

变量be应包含mystring中空格字符的位置。 word2最终应包含quick 另一个解决方案是使用循环“遍历”字符串,但这会欺骗strchr函数。

2 个答案:

答案 0 :(得分:1)

为了得到第二个字,请检查

char mystring[] = "The quick brown fox jumps over the lazy dog";
char word1[] = "The";
char * posb, pose;
char * word2;
int b, e, n;

n = memcmp(mystring, word1, sizeof(word1)-1);
if (n == 0) printf("the first word is found!\n");
posb = strchr(mystring, ' ');   // posb will be a pointer to the first character

b = posb - mystring + 1;

printf("posb -> %d\n", posb);
printf("b -> %d\n", b);
posb = strchr(posb + 1, ' ');   // pose will be a pointer to the second character
printf("calc e\n");

e = posb - mystring + 1;
printf("e -> %d\n", e);

word2 = (char*)malloc(sizeof(char)*(e - b));
memcpy(word2, mystring + b, e - b);
word2[e-b-1] = '\0';
printf("%s:%i:%i:%i\n", word2, b, e, e - b);
free(word2);

答案 1 :(得分:0)

当在一行中搜索单词的出现时,你需要一个指针来指向行中的每个字符,一个搜索词,当前字符与 first <的比较/ em> 搜索字词中的字符,如果匹配则进一步比较,然后根据需要限制比较的一组分隔符,例如,如果搜索'the'仅匹配'the'而不是'they''them''then''there'等......

您可以使用基本算法来确保您不会尝试检查比搜索词长度等更短的行。一个简短示例(更改初始{{1例如,在您的行中{}为'The',您可以执行与以下类似的操作。请注意,搜索中只使用了基本的优化,并不是一个详尽的例子:

'the'

使用/输出

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

int main (int argc, char **argv)
{
    char line[] = "the quick brown fox jumps over the lazy dog.";
    char *p = line;
    char *sterm = argc > 1 ? argv[1] : "the";
    char *delim = " \t\n\'\".";
    size_t llen = strlen (line);
    size_t count = 0, slen = strlen (sterm);

    printf ("\n searching line for '%s'\n\n", sterm);

    for (;p < (line + llen - slen + 1); p++) {

        if (*p != *sterm)
            continue;   /* char != first char in sterm  */
        if (p > line && !strchr (delim, *(p - 1)))
            continue;   /* prior char is not a delim    */
        if (!strchr (delim, *(p + slen)))
            continue;   /* next char is not a delim     */
        if (strncmp (p, sterm, slen))
            continue;   /* chars don't match sterm      */

        printf ("   match %2zu. '%s' at location %zu\n",
                ++count, sterm, p - line);
    }

    printf ("\n total occurrences of '%s' in line : %zu\n\n",
            sterm, count);

    return 0;
}

请注意$ ./bin/searchline searching line for 'the' match 1. 'the' at location 0 match 2. 'the' at location 31 total occurrences of 'the' in line : 2 $ ./bin/searchline fox searching line for 'fox' match 1. 'fox' at location 16 total occurrences of 'fox' in line : 1 使用分隔符字符串strchr,使用当前字符作为delim。查看示例,如果您有任何疑问,请告诉我。你的确切目标有点不清楚,所以如果我错过了你的意图,请告诉我。

使用数组索引而不是指针

如果您更习惯使用字符数组索引而不是指针,则可以始终删除指针char并相应地调整循环逻辑:

p