第一次出现返回strstr函数

时间:2015-02-27 03:20:09

标签: c arrays strstr

继上一个问题之后,现在看来我的代码只从cInput输出第一次出现的cArray。有没有办法让strstr返回所有的事件,而不是在第一次停止程序?非常感谢。

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

#define MAX_STR_LEN 120

int main(){
char *cArray[MAX_STR_LEN] = {"example", "this"};
char cInput[MAX_STR_LEN] = "";
char cOutput[MAX_STR_LEN] = "";

printf("Type your message:\n");
for (int y=0; y<1; y++){
    fgets(cInput, MAX_STR_LEN, stdin);
    char * ptr = cInput;
    while((ptr=strstr(ptr, *cArray)) != NULL){
        strncpy(cOutput, ptr, strlen(*cArray));
        printf("Initialised string array:\n%s\n", cOutput);
        ptr++;
        }
    }
}

输出:

Type your message:
this is an example
Initialised string array:
example
Program ended with exit code: 0

编辑代码:

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

#define MAX_STR_LEN 120

int main() {

char *cArray[MAX_STR_LEN] = { "example", "this", "is", "an" };
char cInput[MAX_STR_LEN] = { 0 };
int y = 0;

printf("Type your message:\n");
fgets(cInput, MAX_STR_LEN, stdin);
cInput[strlen(cInput) - 1] = 0;     /* strip newline from input */
printf("\nInitialised string array:\n");

while (cArray[y])
{
    char * ptr = cInput;
    while ((ptr = strstr(ptr, cArray[y])) != NULL)
    {
        char *ep = strchr (ptr, ' ');
        if (ep) *ep = 0;              /* null-terminate at space */
        printf("%s\n", ptr++);
        if (ep) *ep = ' ';            /* put the space back      */
    }
    y++;
}

return 0;
}

新输出:

Type your message:
this is an example

Initialised string array:
example
this
is
is
an
Program ended with exit code: 0

2 个答案:

答案 0 :(得分:0)

这是更正的程序:

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

#define MAX_STR_LEN 120

int main(){
    char *cArray[MAX_STR_LEN] = { "example", "this" };
    char cInput[MAX_STR_LEN] = "";
    char cOutput[MAX_STR_LEN] = "";

    printf("Type your message:\n");
    fgets(cInput, MAX_STR_LEN, stdin);
    printf("Initialised string array:\n");
    for (int y = 0; y <= 1; y++){
        char * ptr = cInput;
        while ((ptr = strstr(ptr, cArray[y])) != NULL){
            strncpy(cOutput, ptr, strlen(cArray[y]));
            printf("[%d] - %s\n", y, cOutput);
            memset(cOutput, 0, sizeof(cOutput));
            ptr++;
        }
    }
    getchar();
    return 0;
}

<强>更改

  1. 使用memset重置cOutput数组,以便在比较期间 在下一次迭代中,它是空的。
  2. 使用索引y跟踪数组中的每个单词
  3. Chagned数组范围从y<1y<=1

答案 1 :(得分:0)

除了其他答案之外,这里还有一个消除中间变量并稍微重新排列循环逻辑的变体。有很多方法可以解决返回所有strstr次匹配的问题,因此您可以灵活地使用任何符合您需求的方式:

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

#define MAX_STR_LEN 120

int main() {

    char *cArray[MAX_STR_LEN] = { "example", "this" };
    char cInput[MAX_STR_LEN] = { 0 };
    int y = 0;

    printf("Type your message:\n");
    fgets(cInput, MAX_STR_LEN, stdin);
    cInput[strlen(cInput) - 1] = 0;     /* strip newline from input */

    while (cArray[y]) 
    {
        char * ptr = cInput;
        while ((ptr = strstr(ptr, cArray[y])) != NULL)
            printf("\nInitialised string array:\n%s\n", ptr++);
        y++;
    }

    return 0;
}

<强>输出:

alchemy:~/dev/src-c/tmp> ./bin/strstrmulti
Type your message:
my example of this example for fun

Initialised string array:
example of this example for fun

Initialised string array:
example for fun

Initialised string array:
this example for fun

要仅隔离cArray中的单词匹配,您可以使用strchr来测试返回strstr的指针后输入字符串中是否还有空格。如果是这样,只需空出终止显示下一个空格的字符串,这将仅隔离搜索到的单词。如果我误解了评论,只要留下另一个,我很乐意提供帮助。 (仅代码更改)

    while (cArray[y]) 
    {
        char * ptr = cInput;
        while ((ptr = strstr(ptr, cArray[y])) != NULL)
        {
            char *ep = strchr (ptr, ' ');
            if (ep) *ep = 0;              /* null-terminate at space */
            printf("\nInitialised string array:\n%s\n", ptr++);
            if (ep) *ep = ' ';            /* put the space back      */
        }
        y++;
    }

隔离输出:

alchemy:~/dev/src-c/tmp> ./bin/strstrmulti
Type your message:
my example of this example for fun

Initialised string array:
example

Initialised string array:
example

Initialised string array:
this

以下是使用inch-worm方法查看整个单词的一些代码。我没有太多时间去研究所有潜在的陷阱,但这应该让你去。只需替换上一版本中的代码:

    while (cArray[y])
    {
        char *ptr = cInput;
        char *ep = NULL;

        while ((ep = strchr (ptr, ' ')))  /* set end-pointer to each space in input */
        {
            *ep = 0;                      /* null-terminate and compare word        */
            if (strcmp (ptr, cArray[y]) == 0)
                printf("\nInitialised string array:\n%s\n", ptr);
            *ep = ' ';                    /* restore space in string                */
            ptr = ++ep;                   /* set pointer to next char after space   */
        }
        if (strcmp (ptr, cArray[y]) == 0) /* compare last word in input             */
            printf("\nInitialised string array:\n%s\n", ptr);
        y++;
    }

输出w /更新cArray

alchemy:~/dev/src-c/tmp> ./bin/strstrmulti
Type your message:
this is an example to test the example for fun

Initialised string array:
example

Initialised string array:
example

Initialised string array:
this

Initialised string array:
is

Initialised string array:
an