编写函数在一些字符串中查找某个字符:
有2个地方需要注意:
1、我们给指针数组结尾添加了一个NULL指针,遍历查找字符的时候作为循环条件,这样可以免去计算字符数组长度从而确定循环次数。
2、字符串数组的定义
算法1:
#include <stdio.h>
int find_char(char ** strings, char value) {
char * str = NULL;
while((str = *strings++) != NULL) {
printf("\n%s\n", str);
while(*str != '\0') {
printf("--->%c",*str);
if(*str++ == value) {
return 1;
}
}
}
return 0;
}
int main () {
//注意字符串数组的定义
char *strings[] = {"COM", "NET", "ORG", "INFO", "EDU", 0};
int rs = find_char(strings, 'G');
printf("\nrs: %d\n", rs);
int rs2 = find_char(strings, 'X');
printf("\nrs2: %d\n", rs2);
return 0;
}
输出: