1

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

#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

4

2 回答 2

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);
于 2015-12-23T11:58:29.987 回答
0

当在一行中搜索某个单词的出现时,您需要一个指针来遍历该行中的每个字符、一个搜索词、将当前字符与搜索词中的第一个字符进行比较,如果匹配则进一步比较,以及然后是一组分隔符,用于根据需要限制比较,例如,如果只搜索 match而不是, , ,等...'the''the''they''them''then''there'

您可以使用基本算术来确保您不会尝试检查比搜索词长度更短的行等。一个简短的示例(出于示例目的将首字母更改'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并相应地调整循环逻辑:

    for (i = 0; i < (llen - slen + 1); i++) {

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

        printf ("   match %2zu. '%s' at location %zu\n",
                ++count, sterm, &line[i] - line);
    }
于 2016-01-30T21:17:58.633 回答