6.7自己实现字符串操作api一

本文档展示了如何使用C语言自定义字符串操作函数,包括myputs()用于输出字符串,mygets()用于读取用户输入,myStrlen()计算字符串长度,以及myMemset()填充字符数组。在main()函数中,这些函数得到了实际应用,例如分配内存、填充字符串并打印结果。

6.7自己实现字符串操作api一

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

	/*	6.7
	author:edcfreedom
	date:2021/8/23
	funDescription:
	自己实现字符串的api
	
*/

void myputs(char *p)//* 除了变量声明,定义意外,都是运算符
{
	//字符串明显的特点:结尾有'\0'
	while(*p != '\0'){
		//putchar(*p++);
		printf("%c",*p++);
	}
	putchar('\n');
}

int mygets(char *p)
{
	int cnt = 0;
	
	if(p == NULL){
		printf("内存非法");
		return 0;
	}
	while(*p = getchar()){
		if(*p == '\n'){
			return cnt;
			//return;
		}else{
			cnt++;
			p++;
		}
	}
	
}

int myStrlen(char *str)
{
	int cnt = 0;
	while(*str++ != '\0'){
		cnt++;
	}
	return cnt;
	
}

void myMemset(char *p,char c, int size)
{
	while(size){
		
		*p++ = c;
		size--;
	}
}


int main()
{
	//char str[128] = {'\0'};
	char *str = NULL;
	str = (char *)malloc(128);
	memset(str,'v',128);
	str[128] = '\0';
	myputs(str);
	
	char *p = "chenguanxi";
	
	printf("长度:%d\n",myStrlen(p));
	
	myputs(p);
	
	myputs("请输入你的字符串:");
	int n = mygets(str);
	printf("你输入的字节数为:%d\n",n);
	myputs(str);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值