/* 这是一个字符串查找程序 改编自《算法:c语言实现》P64 增加了一个对连续字符的检测的判别 例如查找aaa 但是你输入的是aaaa,其实只有一个aaa */ #include "stdio.h" #include "stdlib.h" #include "time.h" #define N 10000 int main(int argc, char *argv[]) { int i,j,t,temp[N],*ti=temp; char a[N], *p = argv[1]; for (i = 0; i < N; ++i) { temp[i]=-1; } for ( i = 0; i < N-1; a[i] = t, i++) { if ((t = getchar()) == '.' ) { break; } } a[i] = 0; printf("%s\n",a); for ( i = 0; a[i] != 0; i++) { for ( j = 0; p[j] != 0; j++) { if (a[i+j] != p[j] ) { break; } } if (p[j] == 0 ) { *(ti++) = i; } } <span style="white-space:pre"> </span>//对连续字符检测部分 ti = temp; int times =0 ; for (; *ti != -1; ti++) { if (*ti-*(ti-1) ==1) { if (times <3) { times++; } else { printf("%d ", *ti); times = 1; } } else { printf("%d ", *ti); } } return 0; }
原文地址:http://blog.csdn.net/dongfangxy/article/details/33365895