1)strcmp
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str[]="hello",str1[]="hel";
char *p=str;
char *p1=str1;
while(*p || *p1)
{
if(*p!=*p1)
{
*p=*p-*p1;
printf("%d\n",*p);
break;
}
else
{
*p++;
*p1++;
}
}
return 0;
}
得出结果如下:
ubuntu@ubuntu:作业$ gcc 8.c
ubuntu@ubuntu:作业$ ./a.out
108
ubuntu@ubuntu:作业$
2)strcat
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str[]="hello",str1[]="world";
char *p=str;
char *p1=str1;
while(*p)
{
p++;
}
while(*p1)
{
*p++=*p1++;
}
*p=0;
puts(str);
return 0;
}
结果如下:
ubuntu@ubuntu:作业$ gcc 8.c
ubuntu@ubuntu:作业$ ./a.out
helloworld
ubuntu@ubuntu:作业$