112A – Petya and Strings : Codeforces Solution

A. Petya and Strings

time limit per test:2 seconds
memory limit per test:256 megabytes
input/output :standard input /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

#include <iostream>
#include <cstring>
#include <cctype>
 
using namespace std;
 
int main(){
    char str1[100], str2[100];
    int result;
    cin >> str1 >> str2; 
    for(int i=0;i<strlen(str1);i++){
        str1[i]=tolower(str1[i]);
        str2[i]=tolower(str2[i]);
        if(str1[i]<str2[i]){
            cout << "-1";
            exit(0);
        }
        else if (str1[i]>str2[i])
        {
            cout << "1";
            exit(0);
        }
        else
        result =0;        
    }
    cout << result;
      
    return 0;
}

Hints : In this codeforces solution my first attempt was to get the sum of ASCII value of the two strings and compare that. This would give correct result for some test cases but having different character but resulting on the same ASCII sum gives the incorrect answer then I realize on this codeforces solution i need to compare each character and the resulting ASCII value. if the value on the same position is less than the answer is different whereas if the ASCII value in the same position is same then the result is obtained. To solve all these either we need to change all the characters either to uppercase or to lowercase.

Related posts

Leave a Comment