28_函数返回值

来源:互联网 发布:ubuntu装锐捷修改文件 编辑:程序博客网 时间:2024/06/05 15:53
//_28_函数返回值//_28_main.cpp#include <stdio.h>#include <stdlib.h>//功能函数:返回在第一个参数内第二个参数的开始位置//如果在第一个参数内没有发现第二个参数,则返回-1int find_substr(char *,char *);int main(){if(find_substr("C is fun","is")!=-1)printf("Substring is found.\n");elseprintf("Substring is not found.\n");system("pause");return 0;}int find_substr(char *s1,char *s2){register int t;//寄存器变量char *p1,*p2;for(t=0;s1[t];t++){p1 = &s1[t];//在第一个参数中一个一个的寻找p2 = s2;//把s2的第一个字符赋给p2while(*p2 && *p2==*p1)//第一个条件是要保证p2不会溢出{p1++;p2++;}if(! *p2)return t;   //一直向后寻找,直到p2终结符}return -1;}

0 0