सीधे मुख्य सामग्री पर जाएं

HakcerRank Java Strings Introduction Solution

 HakcerRank Java Strings Introduction Solution 


Problem:

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,  and , perform the following operations:

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

Input Format

The first line contains a string . The second line contains another string . 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  and .
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  and  and print them on a single line, separated by a space.

Sample Input 0

hello
java

Sample Output 0

9
No
Hello Java
Solution 1:
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String A = scanner.next();
        String B = scanner.next();
        
        System.out.println(A.length()+B.length());
        
        if(A.compareTo(B)>0){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
        
        A=(A.substring(0,1)).toUpperCase()+A.substring(1);
        B=(B.substring(0,1)).toUpperCase()+B.substring(1);
        
        System.out.println(A+" "+B);
        

    }
    
}

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

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);
        }
    }
}




Solution 4:


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();
        int x=A.length()+B.length();
        System.out.println(x);
        int y=A.compareTo(B);
        if(y>0)
            System.out.println("Yes");
        else
            System.out.println("No");
        
        String ab=A.substring(0,1).toUpperCase();
         String ab1=B.substring(0,1).toUpperCase();
        System.out.println(ab+A.substring(1)+" "+ab1+B.substring(1));
    }
}


Solution 5:


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

public class Solution {
    
    public static String AIsLarger(String A, String B) {
        return A.compareTo(B) > 0 ? "Yes" : "No";
    }

    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()));
        System.out.println(AIsLarger(A, B));
        
        System.out.println(A.substring(0, 1).toUpperCase() + A.substring(1) + " " + B.substring(0, 1).toUpperCase() + B.substring(1));
        
    }
}



टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट