#include<stdio.h>
#include<string.h>
#include<assert.h>
char* strcpy(char * destination, const char * source );
char* strcat ( char * destination, const char * source );
int strcmp ( const char * str1, const char * str2 );
int mystrncmp( const char *string1, const char *string2, size_t count );
char* mystrncpy( char *strDest, const char *strSource, size_t count );
char* mystrncat( char *strDest, const char *strSource, size_t count );
//字符串拷贝函数
char* mystrcpy(char* destination,const char*source){
assert(destination!=NULL&&source!=NULL);
char* dest=destination;
const char* src=source;
while(*src!='\0'){
*dest++=*src++;
}
*dest=*src;
return destination;
}
//字符串连接函数
char* mystrcat(char*destination,const char*source){
assert(destination!=NULL&&source!=NULL);
char* dest=destination;
const char * src=source;
while(*dest!='\0'){
dest++;
}
while(*src!='\0'){
*dest++=*src++;
}
*dest=*src;
return destination;
}
//字符串大小比较函数
int mystrcmp(const char *destination,const char* source){
assert(destination!=NULL&&source!=NULL);
while(*destination!='\0'||*source!='\0'){
if((*destination-*source)!=0)
break;
destination++;
source++;
}
return *destination-*source ;
}
//n个字符串比较
int my_strncmp( const char *string1, const char *string2, size_t count )
{
assert(string1!=NULL && string2!=NULL);
while(count-- != 0)
{
if(*string1 - *string2 != 0)
break;
string1++;
string2++;
}
if(count==0)
return *--string1 - *--string2;
else
return *string1-*string2;
}
//拷贝n个字符
char* mystrncpy( char *string1, const char *string2, size_t count ){
assert(string1!=NULL&&string2!=NULL);
char* dest=string1;
const char* src=string2;
while(count--!=0){
*dest++=*src++;
}
return string1;
}
//连接n个字符
char* mystrncat( char *string1, const char *string2, size_t count ){
assert(string1!=NULL&&string2!=NULL);
char* dest=string1;
const char*src=string2;
while(*dest!='\0'){
dest++;
}
while(count--!=0){
*dest++=*src++;
}
*dest='\0';
return string1;
}
C语言几种常用的string函数
最新推荐文章于 2024-03-05 17:29:08 发布