字符串的相关函数详解

#include <stdio.h>
#include <string.h>

int my_strlen( const char *str)
{
    int count = 0;
    while(*str)
    {
	count++;
	str++;
    }
    return count;
}

char *my_strcpy(char *dst, const char *src)
{
    if( src == NULL)
    {
	return NULL;
    }

    char *tmp = dst;
    while(*src)
    {
	*dst = *src;
	dst++;
	src++;
    }
    *dst = '\0';
    dst = tmp;
    return dst;
}

char *my_strncpy(char *dst, const char *src, int nbytes)
{
    if( src == NULL)
    {
	return NULL;
    }

    char *tmp = dst;
    while(*src && nbytes)
    {
	*dst = *src;
	dst++;
	src++;
	nbytes--;
    }
    *dst = '\0';
    dst = tmp;
    return dst;
}

char *my_strcat(char *dst, const char *src)
{
    if( src == NULL || dst == NULL)
    {
	return NULL;
    }

    char *tmp = dst;
    while(*dst)
    {
	dst++;
    }
    while(*src)
    {
	*dst = *src;
	dst++;
	src++;
    }
    *dst = '\0';
    dst = tmp;
    return dst;
}

int my_strcmp(const char *s1, const char *s2)
{
    while(*s1 == *s2)
    {
	if(*s1 == '\0')
	{
	    return 0;
	}
	if(s1 < s2)
	{
	    return -1;
	}
	s1++;
	s2++;
    }
    return 1;
}

int main()
{
    char str[1000];
    char src_str[1000];
    char dst_str[1000];
    int cmd = 0;
    int str_len = 0;
    int nbytes = 0;
    int ret;
       
    while(1)
    {
        printf("=================show the func=================\n");
        printf("1.calc the str len\n");
        printf("2.copy  the src_str to dst_str\n");
        printf("3.copy  the src_str n bytes to dst_str\n");
        printf("4.link the src_str to dst_str\n");
        printf("5.compare  the str1 and  str2\n");
        printf("===============================================\n");
        
	printf("please input your cmd:\n");
        scanf("%d",&cmd);

	if (0 == cmd)
	{
	    break;
	}

	switch(cmd)
	{
	    case 1:
		{
		    printf("input your scr_str:\n");
        	    scanf("%s",str);
                    str_len = my_strlen(str);
         	    printf("str_len = %d\n",str_len);
          	   // printf("%d\n",strlen(str));
		}break;
	    case 2:
		{
		    printf("input your scr_str:\n");
        	    scanf("%s",src_str);
		    my_strcpy( dst_str,src_str);
         	    printf("%s\n",dst_str);

		}break;
	    case 3:
		{
		    printf("input your scr_str:\n");
        	    scanf("%s",src_str);
		    printf("input the bytes you want to copy:\n");
		    scanf("%d",&nbytes);
		    my_strncpy( dst_str,src_str,nbytes);
         	    printf("%s\n",dst_str);

		}break;
	    case 4:
		{
		    printf("input your scr_str:\n");
        	    scanf("%s",src_str);
		    printf("input your dst_str:\n");
        	    scanf("%s",dst_str);
		    my_strcat( dst_str,src_str);
         	    printf("dst_str = %s\n",dst_str);

		}break;
	    case 5:
		{
		    printf("input your scr_str:\n");
        	    scanf("%s",src_str);
		    printf("input your dst_str:\n");
        	    scanf("%s",dst_str);
		    ret = my_strcmp( dst_str,src_str);
         	    printf("ret = %d\n",ret);

		}break;
	}
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值