Java String Introduction - Hacker Rank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Java String Introduction Problem with a very easy explanation. In this article, you will get more than one approach to solve this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Java String Introduction - Problem Statement
"A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable." — Wikipedia: String (computer science)
This exercise is to test your understanding of Java Strings. A sample String declaration:
String myString = "Hello World!" {codeBox}
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:
- 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.
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 A of 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
hello java {codeBox}
Sample Output
9 No Hello Java {codeBox}
Explanation
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, A is not greater than B 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 String Introduction - Hacker Rank Solution
Approach I:
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Save input */
Scanner scan = new Scanner(System.in);
String A = scan.next();
String B = scan.next();
scan.close();
/* Sum lengths */
System.out.println(A.length() + B.length());
/* Compare lexicographical ordering */
System.out.println(A.compareTo(B) > 0 ? "Yes" : "No");
/* Print the Strings in desired format */
System.out.println(capFirstLetter(A) + " " + capFirstLetter(B));
}
private static String capFirstLetter(String str) {
if (str == null || str.length() == 0) {
return "";
} else {
return str.substring(0,1).toUpperCase() + str.substring(1);
}
}
}
Disclaimer: The above Problem ( Java String Introduction ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.
I hope you have understood the solution to this HackerRank Problem. All these four solutions will pass all the test cases. Now visit Java String Introduction HackerRank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Java