A. Petya and Strings | codeforces 112A | iamAvX

                                                          A. Petya and Strings

          

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.



Solution:


  1. import java.util.Scanner;
  2.  
  3. /**
  4. *
  5. * @author abhishekraj
  6. */
  7. public class Code {
  8.  
  9. /**
  10. * @param args the command line arguments
  11. */
  12. public static void main(String[] args) {
  13. // TODO code application logic here
  14. Scanner sc = new Scanner(System.in);
  15. java.lang.String str1 = sc.nextLine();
  16. java.lang.String str2 = sc.nextLine();
  17. str1 = str1.toLowerCase();
  18. str2 = str2.toLowerCase();
  19. int m=0;
  20. int p1 = 0,p2 = 0;
  21. for(int n=0;n<str1.length();n++)
  22. {
  23. char c1 = str1.charAt(n);
  24. char c2 = str2.charAt(m);
  25. p1 = (int)c1;
  26. p2 = (int)c2;
  27. if(p1<p2)
  28. {
  29. break;
  30. }
  31. else if(p1>p2)
  32. {
  33. break;
  34. }
  35. else
  36. {
  37. m++;
  38. }
  39. }
  40. if(p1<p2)
  41. {
  42. System.out.println("-1");
  43. }
  44. else if(p1>p2)
  45. {
  46. System.out.println("1");
  47. }
  48. else
  49. {
  50. System.out.println("0");
  51. }
  52. }
  53. }

Follow me:-
Twitter :-@RightHacker
Github :- iamavx
instagram:- mr_abhix


Comments

Popular posts from this blog

How to make a simple voting app in Android studio

What is Git and and Github?