1、strcpy
strcpy 函数 :
char *strcpy(char *strDes, const char *strSrc)
{
char *address = strDes;
assert(((strDes != NULL) && (strSrc != NULL)));
while((*strDes++ = *strSrc++) != '\0')
;
return address;
}
strcpy 函数在vs2012测试例子:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int main()
{
char *strcpy(char *strDes, const char *strSrc);
char src[20] = "Tomato";
char des[20] = "Potato";
printf("Des: %s\tSrc: %s",des,src);
strcpy(des,src);
printf("\n");
printf("Des: %s\tSrc: %s",des,src);
system("pause");
}
char *strcpy(char *strDes, const char *strSrc)
{
char *address = strDes;
assert(((strDes != NULL) && (strSrc != NULL)));
while((*strDes++ = *strSrc++) != '\0')
;
return address;
}
运行结果:
2、strcnpy
strncpy 函数 :
char *strncpy(char *strDes,const char *strSrc,int count)
{
char *address = strDes;
assert(strSrc != NULL && strDes != NULL);
while(count-- && *strSrc != '\0')
*strDes++ = *strSrc++;
return address;
}
strncpy 函数在vs2012测试例子:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int main()
{
char *strncpy(char *strDes,const char *strSrc,int count);
char src[20] = "abcdefghi";
char des[20] = "123456789";
printf("Des: %s\tSrc: %s",des,src);
strncpy(des,src,5);;
printf("\n");
printf("Des: %s\tSrc: %s",des,src);
system("pause");
}
char *strncpy(char *strDes,const char *strSrc,int count)
{
char *address = strDes;
assert(strSrc != NULL && strDes != NULL);
while(count-- && *strSrc != '\0')
*strDes++ = *strSrc++;
return address;
}
运行结果:
3、strcat
strcat 函数 :
char *strcat(char *strDes, const char *strSrc)
{
char *address = strDes;
assert((strDes != NULL) && (strSrc != NULL));
while(*strDes != '\0')
++ strDes;
while((*strDes ++ = *strSrc++) != '\0')
NULL;
return address;
}
strcat 函数在vs2012测试例子:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int main()
{
char *strcat(char *strDes, const char *strSrc);
char src[20] = "melon";
char des[20] = "water";
printf("Des: %s\t\tSrc: %s",des,src);
strcat(des,src);
printf("\n");
printf("Des: %s\t\tSrc: %s",des,src);
system("pause");
}
char *strcat(char *strDes, const char *strSrc)
{
char *address = strDes;
assert((strDes != NULL) && (strSrc != NULL));
while(*strDes != '\0')
++ strDes;
while((*strDes ++ = *strSrc++) != '\0')
NULL;
return address;
}
运行结果:
4、strncat
strncat 函数 :
char *strncat(char *strDes, const char *strSrc, int count)
{
char *address = strDes;
assert(strDes != NULL && strSrc != NULL);
while(*strDes != '\0')
++strDes;
while(count-- && *strSrc != '\0')
*strDes++ = *strSrc++;
*strDes = '\0';
return address;
}
strncat 函数在vs2012测试例子:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int main()
{
char *strncat(char *strDes, const char *strSrc, int count);
char src[20] = "marksman";
char des[20] = "water";
printf("Des: %s\t\tSrc: %s",des,src);
strncat(des,src,4);
printf("\n");
printf("Des: %s\t\tSrc: %s",des,src);
system("pause");
}
char *strncat(char *strDes, const char *strSrc, int count)
{
char *address = strDes;
assert(strDes != NULL && strSrc != NULL);
while(*strDes != '\0')
++strDes;
while(count-- && *strSrc != '\0')
*strDes++ = *strSrc++;
*strDes = '\0';
return address;
}
运行结果:
5、strcmp
strcmp 函数 :
int strcmp(const char *s, const char *t)
{
assert(s != NULL && t != NULL);
while(*s && *t && *s == *t)
{
++s;
++t;
}
return(*s - *t);
}
strcmp 函数在vs2012测试例子:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int main()
{
int strcmp(const char *s, const char *t);
char ss[20] = "eggplant";
char tt[20] = "eggshell";
int result = 0;
printf("ss:%s\ntt:%s",ss,tt);
result = strcmp(ss,tt);
printf("\n");
printf("the compare result is : %d",result);
system("pause");
}
int strcmp(const char *s, const char *t)
{
assert(s != NULL && t != NULL);
while(*s && *t && *s == *t)
{
++s;
++t;
}
return(*s - *t);
}
运行结果:
6、strncmp
strcmp 函数 :
int strncmp(const char *s, const char *t, int count)
{
assert((s != NULL) && (t != NULL));
while(*s && *t && *s == *t && count --)
{
++ s;
++ t;
}
return(*s - *t);
}
strcmp 函数在vs2012测试例子:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int main()
{
int strncmp(const char *s, const char *t, int count);
char ss[] = "eggplant";
char tt[] = "eggshell";
int result = -1;
printf("ss:%s\ntt:%s",ss,tt);
result = strncmp(ss,tt,2);
printf("\n");
printf("the compart result is : %d",result);
system("pause");
}
int strncmp(const char *s, const char *t, int count)
{
assert((s != NULL) && (t != NULL));
while(*s && *t && *s == *t && count --)
{
++ s;
++ t;
}
return(*s - *t);
}
运行结果:
7、总结
1、三对字符串函数:字符串复制函数(strcpy,strncpy)、字符串连接函数(strcat,strncat)、字符串比较函数(strcmp,strncmp);
2、字符串复制函数和字符串连接函数返回值类型是 char*,而字符串比较函数返回值类型是int.