最简单的字符串查找

最简单的字符串查找

int  find( char *  str,  char *  sub){ // str2 字串
     int  i = 0 , j = 0 ;
    
int  s1 = strlen(str), s2 = strlen(sub); // sizeof(str)=4
     while (i < s1  &&  j < s2){
        
if (str[i ++ ] == sub[j ++ ])
            
continue ;
        
else {
            i
= i - j + 1 ; j = 0 ;
        }
    }
    
if (j == strlen(sub))
        
return  i - j;
    
else   return   - 1 ;
}

你可能感兴趣的:(最简单的字符串查找)