自己实现字符串操作api一
#include <stdio.h>
#include <stdlib.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;
}
while(*p=getchar()){
if(*p=='\n'){
return cnt;
}else{
cnt++;
p++;
}
}
}
int mystrlen(char *str)
{
int cnt = 0;
while(*str != '\0'){
cnt++;
str++;
}
return cnt;
}
void myMemset(char *p,char c,int size)
{
while(size){
*p++;
size--;
}
}
int main()
{
char *str = NULL;
str = (char *)malloc(128);
myMemset(str,'a',128);
str[128] = '\0';
myputs(str);
char *p = "chenlichen";
printf("长度;%d\n",mystrlen(p));
myputs(p);
myputs("请输入你的字符串;");
int n = mygets(str);
printf("你输入的字节数是;%d\n",n);
myputs(str);
system("pause");
return 0;
}
自己实现字符串操作api 二
#include <stdio.h>
#include <stdlib.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;
}
while(*p=getchar()){
if(*p=='\n'){
return cnt;
}else{
cnt++;
p++;
}
}
}
int mystrlen(char *str)
{
int cnt = 0;
while(*str != '\0'){
cnt++;
str++;
}
return cnt;
}
void myMemset(char *p,char c,int size)
{
while(size){
*p++;
size--;
}
}
int main()
{
char *str = NULL;
str = (char *)malloc(128);
myMemset(str,'a',128);
str[128] = '\0';
myputs(str);
char *p = "chenlichen";
printf("长度;%d\n",mystrlen(p));
myputs(p);
myputs("请输入你的字符串;");
int n = mygets(str);
printf("你输入的字节数是;%d\n",n);
myputs(str);
system("pause");
return 0;
}