Codeforces刷题之路——112A Petya and Strings

A. Petya and Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya loves presents. His mum bought him two strings of the same size for his birthday. The strings consist of uppercase and lowercase Latin letters. Now Petya wants to compare those two strings lexicographically. The letters' case does not matter, that is an uppercase letter is considered equivalent to the corresponding lowercase letter. Help Petya perform the comparison.

Input

Each of the first two lines contains a bought string. The strings' lengths range from 1 to 100 inclusive. It is guaranteed that the strings are of the same length and also consist of uppercase and lowercase Latin letters.

Output

If the first string is less than the second one, print "-1". If the second string is less than the first one, print "1". If the strings are equal, print "0". Note that the letters' case is not taken into consideration when the strings are compared.

Examples
input
aaaa
aaaA
output
0
input
abs
Abz
output
-1
input
abcdefg
AbCdEfF
output
1
Note

If you want more formal information about the lexicographical order (also known as the "dictionary order" or "alphabetical order"), you can visit the following site:

  • http://en.wikipedia.org/wiki/Lexicographical_order


题目大意:比较字符串,不考虑大小写,即A与a同为一个字母,第一个出现字母不相同的字符进行比较,相等输出0,小于输出-1,大于输出1。
解题思路:将其全部转换成小写或者大写,循环寻找第一个不相同的字符,进行大小比较即可。


以下为解题代码(java实现)
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		String str1 = scanner.nextLine().toLowerCase();
		String str2 = scanner.nextLine().toLowerCase();
		
		boolean flag = false;
		char[] ch1 = str1.toCharArray();
		char[] ch2 = str2.toCharArray();
		
		for(int i = 0;i < ch1.length;i++){
			if(ch1[i] == ch2[i]){
				flag = true;
			}else{
				flag = false;
				if(ch1[i] < ch2[i]){
						System.out.println(-1);
				}else{
					System.out.println(1);
				}
				break;
			}
		}
		if(flag){
			System.out.println(0);
		}
		scanner.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值