Java Substring Hackerank Solution

Java Substring Hackerank Solution
Java Substring Hackerank Solution

Java Substring Hackerank Solution

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

Input Format

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


  • 1<=|s|<=100
  • 0<=start<end<=n
  • String s consists of English alphabetic letters (i.e., [a-zA-Z]) only.

Output Format

Print the substring in the inclusive range from start to end-1.

Sample Input

3 7

Sample Output



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

image by hackerrank

Code Solution:

// Java Substring Hackerank Solution

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(;
        String S =;
        int start = in.nextInt();
        int end = in.nextInt();
        char[] ch = new char[S.length()];
        for (int i = 0; i < S.length(); i++) {
            ch[i] = S.charAt(i);
        for(int i=start;i<end;i++){

//Java Substring Hackerank Solution

Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment