strcmp
迎接考試中:
今日必須會(huì)的知識(shí):
1、這是主函數(shù)
//題:比較字符串
//要看ASII表 A:65,a:95
#include<stdio.h>
#include "1.c"
int main(void){
char a[] = "Aaa";
char b[] = "aa";
int num = compare(a,b);
printf("%d",num);
}
1、被調(diào)用的函數(shù)
#include<stdio.h>
int compare(char a[],char b[]){
int bLenth,aLenth;//聲明
int i = 0,j = 0,k = 0,num = 0;
int o;
//計(jì)算字符數(shù)組的長(zhǎng)度
while(a[i]!=''){
i++;
}
aLenth = i;//計(jì)算字符數(shù)組a的長(zhǎng)度
while(b[j]!=''){
j++;
}
bLenth = j;//計(jì)算字符數(shù)組b的長(zhǎng)度
//printf("a數(shù)組的長(zhǎng)度為:%d,b數(shù)組的長(zhǎng)度為:%d
",aLenth+1,bLenth+1);
//誰(shuí)短取誰(shuí)
if(aLenth>=bLenth){
o = bLenth;//aLenth = i;賦值給o;
}else{
o = aLenth;//
}
//
for(k;k<o;k++){
if((int)a[k]>(int)b[k]){//將字符串轉(zhuǎn)換成int型,
return 1;
}else if((int)a[k]==(int)b[k]){
num++;//
continue;
}else{
return -1;
}
}
//
if(num==o){
if(aLenth>bLenth){
return 1;
}else if(aLenth<bLenth){
return -1;
}
else{
return 0;
}
}
}
3、}else if((int)a[k]==(int)b[k]){//我不懂這個(gè)。。。
總結(jié)
- 上一篇: 设置Jexus开机启动
- 下一篇: 割草的男孩(哲理故事)