HackerRank Java- Strings Introduction




Given two strings of lowercase English letters, A and B, perform the following operations:

Sum the lengths of A and B.
Determine if A is lexicographically larger than B (i.e.: does B come before A in the dictionary?).
Capitalize the first letter in A and B and print them on a single line, separated by a space.



  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. /* Save input */
  6. Scanner scan = new Scanner(System.in);
  7. String A = scan.next();
  8. String B = scan.next();
  9. scan.close();
  10. /* Sum lengths */
  11. System.out.println(A.length() + B.length());
  12. /* Compare lexicographical ordering */
  13. System.out.println(A.compareTo(B) > 0 ? "Yes" : "No");
  14. /* Print the Strings in desired format */
  15. System.out.println(capFirstLetter(A) + " " + capFirstLetter(B));
  16. }
  17. private static String capFirstLetter(String str) {
  18. if (str == null || str.length() == 0) {
  19. return "";
  20. } else {
  21. return str.substring(0,1).toUpperCase() + str.substring(1);
  22. }
  23. }
  24. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube