实现函数strlen
#include <stdio.h>
int Strlen ( char * s) {
int count= 0 ;
while ( * s!= '\0' ) {
count++ ;
s++ ;
}
return count;
}
int main ( ) {
char * s, S[ 50 ] ;
int len;
printf ( "请输入一串字符:\n" ) ;
gets ( S) ;
s= S;
len= Strlen ( s) ;
printf ( "该字符串的长度为:%d\n" , len) ;
return 0 ;
}
实现函数atoi
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int Atoi ( char arr[ ] ) {
int index = 0 ;
int num = 0 ;
int flag = 1 ;
if ( arr == NULL )
{
return - 1 ;
}
while ( isspace ( arr[ index] ) )
{
index++ ;
}
if ( arr[ index] == '-' )
{
flag = - 1 ;
}
if ( arr[ index] == '-' || arr[ index] == '+' )
{
index++ ;
}
while ( arr[ index] >= '0' && arr[ index] <= '9' )
{
num = num* 10 + arr[ index] - '0' ;
index++ ;
}
return flag* num;
}
int main ( ) {
int n;
char s[ ] = "-456er12" ;
n= atoi ( s) ;
printf ( "该字符串转换为整数后为(调用库函数):%d\n" , n) ;
n= Atoi ( s) ;
printf ( "该字符串转换为整数后为(自己实现的库函数):%d\n" , n) ;
return 0 ;
}
实现函数itoa
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void invert ( char * p, int N)
{
assert ( p != NULL ) ;
if ( N == 1 || N == 0 )
{
return ;
}
else
{
char ch = p[ 0 ] ;
p[ 0 ] = p[ N- 1 ] ;
p[ N- 1 ] = ch;
invert ( p+ 1 , N- 2 ) ;
}
}
void Itoa ( int num, char * s) {
int index, m, flag, length;
index= m= flag= length= 0 ;
if ( num< 0 ) {
num= - num;
flag= - 1 ;
}
do {
m= num% 10 ;
num= num/ 10 ;
s[ index++ ] = m+ '0' ;
length++ ;
} while ( num) ;
if ( flag== - 1 ) {
s[ index++ ] = '-' ;
length++ ;
}
s[ index] = '\0' ;
invert ( s, length) ;
}
void main ( ) {
int num;
char * s;
s= ( char * ) malloc ( 50 * sizeof ( char ) ) ;
num= + 12356789 ;
printf ( "将%d变成字符串输出后为:" , num) ;
Itoa ( num, s) ;
puts ( s) ;
}
实现函数strcat
#include <stdio.h>
#include <stdlib.h>
char * Strcat ( char * s1, char * s2) {
int i, j;
char * s;
if ( s1== NULL || s2== NULL )
return NULL ;
i= j= 0 ;
s= s1;
while ( s1[ i] )
i++ ;
while ( s2[ j] )
s1[ i++ ] = s2[ j++ ] ;
s1[ i] = '\0' ;
return s;
}
int main ( ) {
char s1[ 10 ] = "123" ;
char s2[ ] = "456" ;
printf ( "s1=%s,s2=%s\n" , s1, s2) ;
Strcat ( s1, s2) ;
printf ( "将s2连接到s1后为:\n" ) ;
printf ( "s1=%s\n" , s1) ;
}
实现函数strncat
#include <stdio.h>
#include <stdlib.h>
char * Strncat ( char * s1, char * s2, int n) {
int i, j;
char * s;
if ( s1== NULL || s2== NULL )
return NULL ;
i= j= 0 ;
s= s1;
while ( s1[ i] )
i++ ;
while ( s2[ j] && j< n)
s1[ i++ ] = s2[ j++ ] ;
s1[ i] = '\0' ;
return s;
}
int main ( ) {
char s1[ 10 ] = "123" ;
char s2[ ] = "4567" ;
printf ( "s1=%s,s2=%s\n" , s1, s2) ;
Strncat ( s1, s2, 2 ) ;
printf ( "将s2的前2个字符连接到s1后为:\n" ) ;
printf ( "s1=%s\n" , s1) ;
}
实现函数strcpy
#include <stdio.h>
char * Strncpy ( char * s1, char * s2) {
int i= 0 ;
char * s;
if ( s1== NULL || s2== NULL )
return NULL ;
s= s1;
while ( s2[ i] )
s1[ i] = s2[ i++ ] ;
s1[ i] = '\0' ;
return s;
}
void main ( ) {
char s1[ 10 ] = "" ;
char s2[ ] = "1234" ;
printf ( "s2中元素为:%s\n" , s2) ;
Strncpy ( s1, s2) ;
printf ( "将s2赋值给s1后:s1=%s\n" , s1) ;
}
实现函数strncpy
#include <stdio.h>
char * Strncpy ( char * s1, char * s2, int n) {
int i= 0 ;
char * s;
if ( s1== NULL || s2== NULL )
return NULL ;
s= s1;
while ( i< n)
s1[ i] = s2[ i++ ] ;
s1[ i] = '\0' ;
return s;
}
void main ( ) {
char s1[ 10 ] = "" ;
char s2[ ] = "1234" ;
printf ( "s2中元素为:%s\n" , s2) ;
Strncpy ( s1, s2, 3 ) ;
printf ( "将s2的前三个元素赋值给s1后:s1=%s\n" , s1) ;
}
实现函数strcmp
#include <stdio.h>
int Strcmp ( char * s, char * c) {
int i= 0 ;
while ( s[ i] && c[ i] ) {
if ( s[ i] > c[ i] )
return 1 ;
else if ( s[ i] < c[ i] )
return - 1 ;
else
i++ ;
}
if ( s[ i] )
return 1 ;
else if ( c[ i] )
return - 1 ;
else
return 0 ;
}
void main ( ) {
char s[ 10 ] , c[ 10 ] ;
printf ( "请输入第一个字符串\n" ) ;
gets ( s) ;
printf ( "请输入第二个字符串\n" ) ;
gets ( c) ;
if ( Strcmp ( s, c) == 1 )
printf ( "%s大于%s\n" , s, c) ;
else if ( Strcmp ( s, c) == - 1 )
printf ( "%s小于%s\n" , s, c) ;
else
printf ( "%s等于%s\n" , s, c) ;
}
实现函数strncmp
#include <stdio.h>
int Strncmp ( char * s, char * c, int n) {
int i= 0 ;
while ( i< n&& s[ i] && c[ i] ) {
if ( s[ i] > c[ i] )
return 1 ;
else if ( s[ i] < c[ i] )
return - 1 ;
else
i++ ;
}
if ( s[ i] == '\0' || c[ i] == '\0' )
return - 2 ;
else
return 0 ;
}
void main ( ) {
int n;
char s[ 10 ] , c[ 10 ] ;
printf ( "请输入第一个字符串\n" ) ;
gets ( s) ;
printf ( "请输入第二个字符串\n" ) ;
gets ( c) ;
printf ( "请输入要比较的字符个数\n" ) ;
scanf ( "%d" , & n) ;
if ( Strncmp ( s, c, n) == 1 )
printf ( "%s的前%d个字符大于%s\n" , s, n, c) ;
else if ( Strncmp ( s, c, n) == - 1 )
printf ( "%s的前%d个字符小于%s\n" , s, n, c) ;
else if ( Strncmp ( s, c, n) == 0 )
printf ( "%s的前%d个字符等于%s\n" , s, n, c) ;
else
printf ( "字符串过短,不能比较\n" ) ;
}