【c语言】利用指针实现strcmpy函数

#include <stdio.h>
//利用指针实现strcmpy函数
int strcmp(char *p1,char *p2) //两个字符串比较函数
{
    int i=0;
    while(*(p1+i)==*(p2+i)){
            i++;
		if (*(p1+i)=='\0'){
		    return(0); //相等时返回结果 0
		} 
	}
		return(*(p1+i)-*(p2+i)); //不等时返回结果为第一个不等字符 ASCII 码的差值
}
void main()
{
    int m;
    char str1[20],str2[20],*p1,*p2;
    printf("input two strings:\n");
    scanf("%s",str1);
    scanf("%s",str2);
    p1=&str1[0];
    p2=&str2[0];
   m=strcmp(p1,p2);
   printf("result:%d,\n",m);
}
发布了52 篇原创文章 · 获赞 69 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/lbqlzch/article/details/88577833