Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java Strings Introduction hackerrank solution

 “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!"

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

hello
java

Sample Output 0

9
No
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,  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”.

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();
System.out.println(A.length()+B.length());
System.out.println(A.compareTo(B)>0?"Yes":"No");
System.out.println(A.substring(0, 1).toUpperCase()+A.substring(1, A.length())+" "+B.substring(0, 1).toUpperCase()+B.substring(1, B.length()));
        
    }
}

Comments