hqyj-base_c-day9

文章展示了如何在C语言中自定义实现字符串长度计算、字符串比较、字符串拷贝和字符串连接四个基本函数。mystrlen遍历字符串直到遇到,mystrcmp逐字符比较直至区分大小或遇到,mystrcpy将源字符串内容复制到目标字符串,mystrcat则在第一个字符串末尾添加第二个字符串内容。
摘要由CSDN通过智能技术生成

mystrlen

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//封装计算字符串长度函数
int mystrlen(char *p)
{
     int i;
    for( i = 0;*(p+i) != '\0';i++)
    {
    }
    return i;
}

int main(int argc, const char *argv[])
{
    //输入字符串
    char a[100];
    printf("输入一串字符\n");
    gets(a);
    //调用计算字符串长度函数
    int len ;
    len = mystrlen(a);
    printf("len = %d\n",len);
    return 0;
}

运行:

ubuntu@ubuntu:day9$ ./a.out 
输入一串字符
238&*(*^^%iuchewGGY U
len = 21

mystrcmp

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//封装字符串比较函数
int mystrcmp(char *p ,char *q)
{
    //首字母开始进行比较,直到两个都'\0'结束,或者比出大小直接结束
    for(int i = 0; (*(p+i) != '\0')||(*(q+i) != '\0');i++)
    {
        if(p[i] > q[i])
        {
            return 1;
            break ;
        }else if (p[i] < q[i])
        {
            return -1;
            break ;
        }else
        {
            continue ;
        }
        return 0 ;
    }
}

int main(int argc, const char *argv[])
{
    //输入两个字符串
    char a[100] , b[100] ;
    printf("请输入第一串字符");
    gets(a);
    printf("请输入第二串字符");
    gets(b);
     //调用字符串比较函数
    int n;
    n = mystrcmp(a,b);
    //输出比较结果
    if(n > 0)
    {
        printf("a > b\n");
    }else if(n<0)
    {
        printf("a < b\n");
    }else
    {
        printf("a = b\n");
    }
    return 0;
}

运行:

ubuntu@ubuntu:day9$ ./a.out 
请输入第一串字符1234
请输入第二串字符12345
a < b
ubuntu@ubuntu:day9$ ./a.out 
请输入第一串字符qwer
请输入第二串字符qwer
a = b
ubuntu@ubuntu:day9$ ./a.out 
请输入第一串字符ytrew
请输入第二串字符ytr
a > b

mystrcpy

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//封装字符串拷贝函数
char *mystrcpy(char *p ,char *q)
{
    int i;
    for(i = 0 ;*(q+i) != 0;i++)
    {
    //判断并进行一一对应赋值;
        *(p+i) = *(q+i);
    }
    //赋值结束用'\0'结尾
    *(p+i) = '\0';
    return p;
}

int main(int argc, const char *argv[])
{
    //定义两个字符串,对第二个赋值
    char a[100] , b[100] ;
    printf("请输入串字符");
    gets(b);
     //调用字符串拷贝函数
    char *p;
    p = mystrcpy(a,b);
    printf("拷贝结果:\n");
    puts(p);
    return 0;
}

运行:

请输入串字符123qwe%&*$ YIIN
拷贝结果:
123qwe%&*$ YIIN

mystrcat

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//封装字符串连接函数
char *mystrcat(char *p,char *q)
{
    int i;
    //判断*p何时截止,并计数
    for(i = 0 ; p[i] != '\0' ;i++);
    //截止之后一一对应赋值*q--->*p
    for(int j = 0 ; q[j] != '\0' ; j++)
    {
        p[i] = q[j];
        i++;
    }
    return p;
}

int main(int argc, const char *argv[])
{
        //输入两个字符串
    char a[100] , b[100] ;
    printf("请输入第一串字符");
    gets(a);
    printf("请输入第二串字符");
    gets(b);
    //调用字符串连接函数
    char *p;
    p = mystrcat(a,b);
    printf("字符串连接结果:\n");
    puts(p);
    return 0;
}

运行:

ubuntu@ubuntu:day9$ ./a.out 
请输入第一串字符hello
请输入第二串字符 world
字符串连接结果:
hello world
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值