Java Strings Introduction Hacker Rank Solution

Hello coders, In this post, you will learn how to solve Java Strings Introduction Hacker Rank Solution. This problem is a part of the Java programming series. 

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

Java Strings Introduction hacker rank solutions
Java Strings Introduction Hacker Rank Solution

Java Strings Introduction Hacker Rank Solution


“A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.

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 A is lexicographically greater than B 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


Sample Output 0

Hello Java

Explanation 0

String A  is “hello” and B is “java”.

A has a length of 5 , and B has a length of 4; the sum of their lengths is 9 .
When sorted alphabetically/lexicographically, “hello” precedes “java”; therefore,  is not greater than  and the answer is No.

When you capitalize the first letter of both A  and B and then print them separated by a space, you get “Hello Java”.

Java Strings Introduction Hacker Rank Solution

import java.util.Scanner;
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        String A =;
        String B =;
        System.out.println(A.length() + B.length());
        if (A.compareTo(B) > 0) {
        } else {
        A = A.substring(0, 1).toUpperCase() + A.substring(1);
        B = B.substring(0, 1).toUpperCase() + B.substring(1);
        System.out.println(A + " " + B);

Disclaimer: The above Problem (Java Strings Introduction) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Sharing Is Caring

Leave a Comment