-
函数式编程
123456789101112131415161718192021222324252627282930313233343536373839#include<stdio.h>#include<string.h>int MyStrCmp( char *s1,char *s2 );int main(){ char s1[30], s2[30]; gets(s1); gets(s2); printf("%d\n", MyStrCmp(s1, s2)); return 0;} int MyStrCmp(char * s1, char * s2){ int flag = 0;//0表示相等,1表示s1大于s2,-1表示s1小于s2 int minLength = 0; //较短串的长度 int i = 0; minLength = strlen(s1) >= strlen(s2) ? strlen(s1) : strlen(s2); for(i = 0; i < minLength; i++) { if(s1[i] != s2[i]) { if(s1[i] > s2[i]) flag = 1; else flag = -1; break; } else continue; } return flag;}
-
繁花如伊
BOOL MyStrCmp(char* a,char* b){return a[0]>=b[0];}
-
有只小跳蛙
#include<stdio.h>#define N 100int MyStrCmp(char * s1, char * s2){int ret, i=0;while(s1[i]!='\0' && s2[i]!='\0' && s1[i]==s2[i])i++;if(s1[i]==0 && s2[i]==0)ret=0; //相等else if(s1[i]==0)ret=-1; //串s1小于串s2else if(s2[i]==0)ret=1; //串s1大于串s2else if(s1[i]>s2[i])ret=1; //串s1大于串s2elseret=-1; //串s1小于串s2return ret;}int main(){char s1[N], s2[N];gets(s1);gets(s2);printf("%s\n%s\n",s1,s2);printf("%d\n", MyStrCmp(s1, s2));return 0;}