strcmp
迎接考試中:
今日必須會的知識:
1、這是主函數
//題:比較字符串
//要看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、被調用的函數
#include<stdio.h>
int compare(char a[],char b[]){
int bLenth,aLenth;//聲明
int i = 0,j = 0,k = 0,num = 0;
int o;
//計算字符數組的長度
while(a[i]!=''){
i++;
}
aLenth = i;//計算字符數組a的長度
while(b[j]!=''){
j++;
}
bLenth = j;//計算字符數組b的長度
//printf("a數組的長度為:%d,b數組的長度為:%d
",aLenth+1,bLenth+1);
//誰短取誰
if(aLenth>=bLenth){
o = bLenth;//aLenth = i;賦值給o;
}else{
o = aLenth;//
}
//
for(k;k<o;k++){
if((int)a[k]>(int)b[k]){//將字符串轉換成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]){//我不懂這個。。。
總結
- 上一篇: 设置Jexus开机启动
- 下一篇: 割草的男孩(哲理故事)