封装strlen
#include<stdio.h>
#include<string.h>
int main()
{
char arr[30]="";
int i=0;
int len=0;
gets(arr);
while(arr[i]!='\0')
{
len++;
i++;
}
printf("%d\n",len);
return 0;
}
封装strcmp
#include<stdio.h>
#include<string.h>
int main()
{
char arr[30]="";
char brr[30]="";
char *pa=arr;
char *pb=brr;
int result=0;
gets(arr);
gets(brr);
int i=0;
for(i=0 ; arr[i]!='\0' || brr[i]!='\0' ; i++)
{
if(*(pa+i)>*(pb+i))
{
result=*(pa+i)-*(pb+i);
break;
}
else if(*(pa+i)==*(pb+i))
{
result=0;
}
else if(*(pa+i)<*(pb+i))
{
result=*(pa+i)-*(pb+i);
break;
}
}
printf("%d\n",result);
return 0;
}
封装strcpy
#include<string.h>
#include<stdio.h>
int main()
{
char arr[30]="hello";
char brr[30]="worldo";
char *pa=arr;
char *pb=brr;
int len=strlen(brr);
int i=0;
char *end=brr+len-1;
while(pb<=end)
{
*pa=*pb;
pa++;
pb++;
}
puts(arr);
puts(brr);
return 0;
}
封装strcat
#include<stdio.h>
#include<string.h>
int main()
{
char arr[30]="hello";
char brr[30]="world";
char *pa=arr;
char *pb=brr;
gets(arr);
gets(brr);
int len_a=strlen(arr);
int len_b=strlen(brr);
char *end_a=arr+len_a-1;
char *end_b=brr+len_b-1;
while(pb<=end_b)
{
*(pa+len_a)=*pb;
pb++;
pa++;
}
puts(arr);
puts(brr);
return 0;
}