实例6 函数的返回值

it2022-05-08  7

//功能:在s1中如果不能找到s2,则返回"Substring is not found.\n",反之. #include <stdio.h> int find_substr(char *s1, char *s2);//函数声明 void main() { if(find_substr("C is fun","un") != -1) { printf("Substring is found.\n"); } else { printf("Substring is not found.\n"); } } int find_substr(char *s1, char *s2) { int t; char *p, *p2; for(t = 0; s1[t]; t++)//字符串常量是存在数组中的s1[0]就是字符串第一个字符 { p = &s1[t]; p2 = s2; while(*p2 && (*p2 == *p))//先比较s1,s2的值是不是相等的,不相等就跳过 //这里的与运算是测试P2是不是到最后字符了就是'\0' { p++; p2++; } if(*p2 == '\0') return t; } return -1; }

转载于:https://www.cnblogs.com/hnrainll/archive/2011/02/18/1958268.html

相关资源:数据结构—成绩单生成器

最新回复(0)