6.7自己实现字符串操作api一
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
void myputs(char *p)
{
while(*p != '\0'){
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;
}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 = 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;
}