字符串出现次数
# include <stdio.h>
char * my_strstr ( char * str1, char * str2)
{ char * f= str1; char * r= str1; char * t= str2; while ( * str1) { r= f; while ( * f== * t&& * f != '\0' ) { f++ ; t++ ; } if ( * t == '\0' ) { return r; } f= r; t= str2; f++ ; } return NULL ; } # include <stdio.h>
int my_strlen ( char * arr)
{ int i= 0 ; while ( arr[ i] != '\0' ) { i++ ; } return i;
}
int main ( )
{ char str1[ ] = "1111abcduuxnixabcdixenxeabcdjjj" ; char str2[ ] = "abcd" ; char * p= my_strstr ( str1, str2) ; int count= 0 ; while ( p!= NULL ) { count++ ; p+= my_strlen ( str2) ; p= my_strstr ( p, str2) ; } printf ( "目标字符串出现次数%d\n" , count) ; return 0 ; }
输出结果:3
统计字符个数
# include <stdio.h>
int getstrcount ( char * ch)
{ int i= 0 ; int count= 0 ; while ( ch[ i] ) { if ( ch[ i] != '\0' ) { count++ ; } i++ ; } return count;
}
int main ( )
{ char ch[ ] = " hello world" ; int len= getstrcount ( ch) ; printf ( "字符个数:%d\n" , len) ; return 0 ;
}
输出结果: 10
# include <stdio.h>
int my_strlen ( char * arr)
{ int i= 0 ; while ( arr[ i] != '\0' ) { i++ ; } return i;
}
int main ( )
{ char arr[ ] = "zxjbxjckncjcejcnecnelcnecrenkl" ; int ch[ 26 ] = { 0 } ; for ( int i= 0 ; i< my_strlen ( arr) ; i++ ) { ch[ arr[ i] - 'a' ] ++ ; } for ( int i= 0 ; i< 26 ; i++ ) { printf ( "字母:%c出现次数%d\n" , i+ 'a' , ch[ i] ) ; } return 0 ;
}
输出结果:字母:
a出现次数0
字母:b出现次数1
字母:c出现次数7
字母:d出现次数0
字母:e出现次数5
字母:f出现次数0
字母:g出现次数0
字母:h出现次数0
字母:i出现次数0
字母:j出现次数4
字母:k出现次数2
字母:l出现次数2
字母:m出现次数0
字母:n出现次数5
字母:o出现次数0
字母:p出现次数0
字母:q出现次数0
字母:r出现次数1
字母:s出现次数0
字母:t出现次数0
字母:u出现次数0
字母:v出现次数0
字母:w出现次数0
字母:x出现次数2
字母:y出现次数0
字母:z出现次数1