算法提高 11-1实现strcmp函数
来源:互联网 发布:hexo 知乎 编辑:程序博客网 时间:2024/06/05 19:48
问题描述
自己实现一个比较字符串大小的函数,也即实现strcmp函数。函数:int myStrcmp(char *s1,char *s2) 按照ASCII顺序比较字符串s1与s2。若s1与s2相等返回0,s1>s2返回1,s1<s2返回-1。具体来说,两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止(注意'\0'值为0,小于任意ASCII字符)。如:
"A"<"B"
"a">"A"
"computer">"compare"
"hello"<"helloworld"
"A"<"B"
"a">"A"
"computer">"compare"
"hello"<"helloworld"
样例输出
数据规模和约定
字符串长度<100。
#include<stdio.h>#include<string.h>int main(){char s1[100],s2[100];scanf("%s%s",s1,s2);int i,len1=strlen(s1),len2=strlen(s2);if(len1==len2){for(i=0;i<len1;i++){if(s1[i]>s2[i]){printf("1");return 0;}if(s1[i]<s2[i]){printf("-1");return 0;}}printf("0");return 0;}if(len1>len2){for(i=0;i<len2;i++){if(s1[i]>s2[i]){printf("1");return 0;}if(s1[i]<s2[i]){printf("-1");return 0;}}printf("1");return 0;}if(len1<len2){for(i=0;i<len1;i++){if(s1[i]>s2[i]){printf("1");return 0;}if(s1[i]<s2[i]){printf("-1");return 0;}}printf("-1");return 0;}return 0;}
0 0
- 算法提高 11-1实现strcmp函数
- 算法提高 11-1实现strcmp函数
- 算法提高 11-1实现strcmp函数
- 算法提高 11-1实现strcmp函数
- 算法提高 11-1实现strcmp函数
- 蓝桥杯 ADV-227 算法提高 11-1实现strcmp函数
- 蓝桥杯 算法提高 11-1实现strcmp函数
- 算法提高 11-1实现strcmp函数 java版
- 蓝桥杯 算法提高 11-1实现strcmp函数
- 蓝桥杯 算法提高 实现strcmp函数
- 蓝桥杯-11-1实现strcmp函数(java)
- strcmp函数实现
- strcmp函数的实现
- 实现strcmp()函数
- strcmp函数的实现
- strcmp函数的实现
- strcmp函数的实现
- 实现strcmp 函数
- linux操作学习08:系统服务的控制(systemctl 命令)
- Codeforces
- LeetCode 500. Keyboard Row
- 【LeetCode】412.Fizz Buzz_EASY(四)
- spark2.1.0版本 SparkSQL相关内容的测试和学习
- 算法提高 11-1实现strcmp函数
- hibernate通过HibernateCallback()调用底层session
- sscanf基本用法
- 用 config drive 配置网络
- support-v4、support-v7 的依赖关系及 Getting java.util.zip.ZipException: duplicate entry exception 的处理
- TensorFlow文本摘要生成
- MFC画图
- 剑指offer34:丑数
- python内置字符串处理函数