Pers.narod.ru. Алгоритмы. Количество вхождений строки в строку |
Условие задачи:
В строке s, заданной указателем, подсчитать количество вхождений строки t, также заданной указателем. Задачу решить с помощью функции
#include <stdio.h>
int strnchr (unsigned char *s, unsigned char *t) {
int n=0;
unsigned char *t0=t;
while (*s) {
while (*s!=*t) {
s++;
if (*s=='\0') return n;
}
while (*s==*t) {
s++; t++;
if (*t=='\0') {
n++;
if (*s=='\0') return n;
}
}
t=t0;
}
return n;
}
void main() {
unsigned char *s="abacbccabbbaacabc";
unsigned char *t="abc";
puts (s);
puts (t);
printf ("%d times",strnchr(s,t));
getchar();
}
|
|