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]!='\0'){
		i++;
	}
	aLenth = i;//计算字符数组a的长度
	while(b[j]!='\0'){
		j++;
	}
	bLenth = j;//计算字符数组b的长度
      //printf("a数组的长度为:%d,b数组的长度为:%d\n",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]){//我不懂这个。。。

 

posted @ 2016-11-03 10:12  一阙梅曲香素笺  阅读(245)  评论(0编辑  收藏  举报