1. 程式人生 > 實用技巧 >[CF從零單排#5]112A - Petya and Strings

[CF從零單排#5]112A - Petya and Strings

題目來源:http://codeforces.com/problemset/problem/112/A

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
inputCopy
aaaa
aaaA
outputCopy
0
inputCopy
abs
Abz
outputCopy
-1
inputCopy
abcdefg
AbCdEfF
outputCopy
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

題目大意:

輸入兩個字串,保證它們都是字母且長度相等。比較時,不區分大小寫。按照字典序進行比較,如果第一個字串更大,結果是1;一樣大,結果是0;如果第二個字串更大,結果是-1。

題目分析:

模擬題,讀入字串,求出字串長度len。從0~len-1掃描一遍,先將兩個字串的字母都轉為小寫,再比較大小。

#include <bits/stdc++.h>
using namespace std;
int main(){
	char s[110], p[110];
	cin >> s >> p;
	int len = strlen(s);
	int k = 0;
	for(int i=0; i<len; i++){
		if(s[i]>='A' && s[i]<='Z')
			s[i] += 'a'-'A';
		if(p[i]>='A' && p[i]<='Z')
			p[i] += 'a'-'A';
		if(s[i]>p[i]){
			k = 1;
			break;
		}else if(s[i]<p[i]){
			k = -1;
			break;
		}
	}
	cout << k;
	return 0;
}