不使用字符串函数函数,实现字符串的求长度,复制,比较,连接。

/*
    不使用字符串函数函数,实现字符串的求长度,复制,比较,连接。
*/
#include <stdio.h>
#define N 4
int main()
{
    #if N == 1
        //求长度
        char ch[20]={0};
        printf("请输入一个字符串:");
        scanf("%s",ch);
        int i = 0;
        while(ch[i]!='\0')
        {
            i++;
        }
        printf("字符串的长度是%d\n",i);
    #elif N == 2
        //实现字符串的复制,将字符串2复制给字符串1
        char ch1[20]={0};
        printf("请输入字符串1:");
        scanf("%s",ch1);
        char ch2[20]={0};
        printf("请输入字符串2(2要比1长):");
        scanf("%s",ch2);
        int i = 0;
        while(ch2[i]!='\0')
        {
            ch1[i]=ch2[i];
            i++;
        }
        ch1[i]='\0';
        printf("复制后的字符串为:%s\n",ch1);
    #elif N == 3
        //实现字符串的连接
        char ch1[20]={0};
        printf("请输入字符串1:");
        scanf("%s",ch1);
        char ch2[20]={0};
        printf("请输入字符串2:");
        scanf("%s",ch2);
        //找到目的串‘\0’的位置
        int i = 0;
        while(ch1[i]!='\0')
        {
            i++;
        }
        int j = 0;
        while(ch2[j]!='\0')
        {
            ch1[i]=ch2[j];
            i++;
            j++;
        }
        ch1[i]='\0';
        printf("拼接后的字符串为:%s\n",ch1);
    #elif N == 4
        //实现字符串的比较
        char ch1[20]={0};
        printf("请输入字符串1:");
        scanf("%s",ch1);
        char ch2[20]={0};
        printf("请输入字符串2:");
        scanf("%s",ch2);
        int i = 0;
        while(ch1[i] || ch2[i])
        {
            if(ch1[i] != ch2[i])
            {
                printf("字符串不相等,%d\n",ch2[i]-ch1[i]);
                return 0;
            }
            i++;
        }
        printf("字符串相等\n");
    #endif
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值