用指针改写strcpy函数
#include <stdio.h>
void my_strcpy(char* str,char* str1);
int main(int argc, const char *argv[])
{
char str[]="hello";
char str1[]="";
my_strcpy(str ,str1);
printf("%s\n",str1);
return 0;
}
void my_strcpy(char* str,char* str1)
{
int i=0;
for(i;*(str+i)!='\0';i++){
*(str1+i)=*(str+i);
}
*(str1+i)='\0';
}
用指针改写strlen函数
#include <stdio.h>
int my_strlen(char* str);
int main(int argc, const char *argv[])
{
char str[]="asdfghhjkkl";
int sum=0;
sum= my_strlen(str);
printf("%d\n",sum);
return 0;
}
int my_strlen(char* str){
int count=0;
char* ptr=str;
for(int i=0;*(ptr+i)!='\0';i++)
{
count ++;
}
return count;
}
用指针改写strcat函数
#include <stdio.h>
2
3 void my_strcat(char* str,char* str1);
4
5 int main(int argc, const char *argv[])
6 {
7 char str[30]="hello";
8 char str1[30]="yes";
9 my_strcat(str,str1);
10 printf("%s\n",str);
11 return 0;
12 }
13
14 void my_strcat(char* str,char* str1){
15 char* ptr=NULL;
16 ptr=str;
17 char* ptr1=str1;
18 int i=0,j=0;
19 for(i=0;*(ptr+i)!='\0';i++){
20 }
21 for(j=0;*(ptr1+j)!='\0';j++){
22 *(ptr+i+j)=*(ptr1+j);
23 }
24 *(ptr+i+j)='\0';
25
26 }
用指针改写函数strcmp
#include <stdio.h>
int my_strcmp(char* str,char* str1);
int main(int argc, const char *argv[])
{
char str[]="hello";
char str1[]="hello";
printf("%d\n",my_strcmp(str,str1));
return 0;
}
int my_strcmp(char* str,char* str1){
int count=0;
int i=0;
for(i;*(str+i)!='\0';i++){
if((*(str+i)-*(str1+i))!=0){
count=*(str+i)-*(str1+i);
}
}
if(*(str1+i)!='\0')
count-=*(str1+i);
return count;
}