Java Strings Introduction Hackerrank Solution

Java Strings Introduction Hackerrank Solution
Java Strings Introduction Hackerrank Solution

Java Strings Introduction Hackerrank Solution

This exercise is to test your understanding of Java Strings. A sample String declaration:

String myString = "Hello World!"

The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.

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

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

Input Format

The first line contains a string A. The second line contains another string B. The strings are comprised of only lowercase English letters.

Output Format

There are three lines of output:
For the first line, sum the lengths of A and B.
For the second line, write Yes if  is lexicographically greater than  otherwise print No instead.
For the third line, capitalize the first letter in both A and  B and print them on a single line, separated by a space.

Sample Input 0

hello
java

Sample Output 0

9
No
Hello Java

Code: Solution:

// Java Strings Introduction Hackerrank Solution

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        String B=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        System.out.println(A.length()+B.length());
       int compare = A.compareTo(B);  

if (compare < 0) {  
    System.out.println("No");
}
else if(compare>0) {
    System.out.println("Yes");
}
else{
    System.out.println("No");
}
String C = A.substring(0, 1).toUpperCase() + A.substring(1);
String D = B.substring(0, 1).toUpperCase() + B.substring(1);
System.out.println(C+" "+D);

    }

}

//Java Strings Introduction Hackerrank Solution

Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment