C语言练习之 字符串 查找字符

  C语言练习,为了更好的掌握指针,我发现如果实现字符查找函数,就可以了,

这里面设计到了二级指针的知识,还有字符数组等,值得练习。

 1 #include <stdio.h>
 2 
 3 
 4 #define TRUE 1
 5 #define FALSE 0
 6 
 7 int find_char(char **strings, char value);
 8 
 9 char *str[] = {
10     "first",
11     "second",
12     "third"
13 };
14 
15 int main(int argc, char *argv) {
16 
17     char c;
18     scanf("%c", &c);
19     if (find_char(str,c))
20     {
21         printf("%c in sourcestr\n", c);
22     }
23     else {
24         printf("%c not sourcestr\n", c);
25     }
26     
27 }
28 
29 
30 int find_char(char **strings, char value) {
31     char *string;
32     while ((string=*strings++)!=NULL)
33     {
34         while (*string!= '\0') {
35             if (*string++ == value) {
36                 return TRUE;
37             }
38         }
39     }
40     return FALSE;
41 }

在VS2015上程序运行如下:

猜你喜欢

转载自www.cnblogs.com/xuelanga000/p/11308076.html