算法提高 字符串比较

独立实现标准字符串库的strcmp函数,即字符串比较函数,从键盘输入两个字符串,按字典序比较大小,前者大于后者输出1,前者小于后者输出-1,两者相等输出0。
样例输入: 
apple one 
样例输出:
-1
样例输入: 
hello he
样例输出:
1

样例输入: 
hello hello
样例输出:
0

主要考察字符串strcmp函数的使用

#include<stdio.h>
#include<string.h>
int main(){
    
    
	char a[100],b[100];
	while(scanf("%s %s",a,b)!=EOF){
    
    
		printf("%d\n",strcmp(a,b));
	}
	return 0;
}

运行结果:

apple one
-1
hello he
1
hello hello
0

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/mjh1667002013/article/details/115214209