Neutron number string string appears calculated.

/ * 
Calculate the number of neutrons string string appears. 
Algorithm: the longest match string, the following is an illustration: 
ABCABCABC 
ABC 
looping through letter string before lenT-lenP; 
loop through the string, with the substring point j, k with the recording position of the letter string begins each trip, if the p [ j] == t [k] is k, j are after the shift 
when the length of the substring j == lenP, a substring is found, the number of ++. 

* / 
#Include <stdio.h> 
#include <math.h> 
#include <malloc.h> 
#include < String .h> 
main () 
{ 
    char T [ 100 ], P [ 100 ]; 
    the gets (T); 
    the gets (P); 
    int I, J, K, C = 0 , Lent, lenP; 
    Lent = strlen (T); 
    lenP = strlen (P);
    for(i=0;i<=lenT-lenP;i++){
        for(j=0,k=i;j<lenP&&P[j]==T[k];k++,j++);
        if(j==lenP) c++;
    }
    printf("%d\n",c);
}

Guess you like

Origin www.cnblogs.com/zhaohuan1996/p/12181015.html