C语言 判断字符串在不在字符串数组中
时间: 2023-06-26 16:08:21 浏览: 249
可以使用strcmp()函数来比较字符串是否相等,然后使用循环遍历字符串数组,判断是否存在该字符串。
示例代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char strArray[10][20] = {"hello", "world", "c", "language", "programming", "is", "fun", "to", "learn", "and"};
char str[20];
int i, flag = 0;
printf("Enter a string: ");
scanf("%s", str);
for (i = 0; i < 10; i++) {
if (strcmp(str, strArray[i]) == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
printf("%s is found in the array.\n", str);
} else {
printf("%s is not found in the array.\n", str);
}
return 0;
}
```
输入一个字符串后,这个程序会检查这个字符串是否在预定义的字符串数组中。如果存在,则输出“is found in the array.”,否则输出“is not found in the array.”。
阅读全文