Thursday, December 30, 2021

JAVA substring Hackerrank

 Given a string, , and two indices,  and , print a substring consisting of all characters in the inclusive range from  to . You'll find the String class' substring method helpful in completing this challenge.

Input Format

The first line contains a single string denoting .
The second line contains two space-separated integers denoting the respective values of  and .

Constraints

  • String  consists of English alphabetic letters (i.e., ) only.

Output Format

Print the substring in the inclusive range from  to .

Sample Input

Helloworld
3 7

Sample Output

lowo

Explanation

In the diagram below, the substring is highlighted in green:

substring.png

Answer:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String S = in.next();
        int start = in.nextInt();
        int end = in.nextInt();
        
//in substring function end index exclusive
        System.out.println(S.substring(start, end));
    }
}

JAVA strings introduction

 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

Explanation 0

String  is "hello" and  is "java".

 has a length of , and  has a length of ; the sum of their lengths is .
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  and  and then print them separated by a space, you get "Hello Java".

Answer:

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. */
        int l1 = A.length();
        int l2 = B.length();
        
        System.out.println((l1+l2));
        
        //compare to compares two strings lexographically returns +ve no. if s1>s2
        System.out.println(A.compareTo(B)>0?"Yes":"No");
        
        //substring fun: in range substring(0,1) 1 i.e, upper range is excluded
        System.out.println(A.substring(0,1).toUpperCase() + A.substring(1,A.length())+" "+ B.substring(0,1).toUpperCase()+B.substring(1,B.length())) ;
    }
}




Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...