常见的c字符串处理函数的源代码以及简单例子【1】

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.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值