c 查找 子字符串
#include <stdio.h> int Search(char *s1, char *s2) { char *temp = s1; int count = 0; while(*temp != '\0') { char *tmp1 = temp; char *t1 = s2; while(*t1 != '\0' && *tmp1 != '\0') { if(*t1 != *tmp1) break; //如果相等,且到最后,返回1 if(*t1 == *tmp1 && *(t1+1) == '\0') return count; //若相等则继续比较下一个字符 if(*t1 == *tmp1) { t1++; tmp1++; } } temp++; count++; } return 0; } int main(int argc, char *argv[]) { //printf("Hello, world\n"); char s1[50] = ""; char s2[50] = ""; printf("input s1:\n"); gets(s1); printf("input s2:\n"); gets(s2); printf("输出结果:%s 包含 %s,位置在:%d\n",s1,s2,Search(s1,s2)); return 0; }