算法提高 11-1实现strcmp函数

问题描述

这里写图片描述

   "A"<"B"
   "a">"A"
   "computer">"compare"
   "hello"<"helloworld"

样例输出
这里写图片描述
数据规模和约定
  字符串长度<100。

代码如下:

package ADV_227;
import java.util.Scanner;
public class Main {
    public static void cmp(char c1[],char c2[]){
        int length1=c1.length;
        int length2=c2.length;
        int i=0;
        int j=0;
        int res=0;
        while(i<length1&&j<length2){
            res=c1[i]-c2[j];
            i++;
            j++;
            if(res!=0)
                break;  
        }
        if(res>0)
            System.out.print(1);
        else if(res<0)
            System.out.print(-1);
        else{
            int temp1=length1-i;
            int temp2=length2-j;
            if(temp1==temp2)
                System.out.print(0);
            else if(temp1>temp2)
                System.out.print(1);
            else
                System.out.print(-1);       
        }   
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        String s1=cin.next();
        String s2=cin.next();
        char c1[]=s1.toCharArray();
        char c2[]=s2.toCharArray();
        cmp(c1,c2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值