小编Dav*_*d78的帖子

c中的数组迭代strstr

我想知道在下面的迭代中是否可以安全地找到数组中第一次出现的str,或者是否有更好的方法.谢谢

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

const char * list[] = {"One","Two","Three","Four","Five"};

char *c(char * str) {
    int i;
    for (i = 0; i < 5; i++) {
        if (strstr(str, list[i]) != NULL) return list[i];
    }
    return "Not Found";
}
int main() {
    char str[] = "This is a simple string of hshhs wo a char";

    printf("%s", c(str));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays iteration strstr

1
推荐指数
1
解决办法
1821
查看次数

标签 统计

arrays ×1

c ×1

iteration ×1

strstr ×1