write a java program to Fetch Middle Characters from String.

Write a program to read a string of even length andĀ to fetch two middlemost characters from the input string and return it as string output.

Input and Output Format

  • Input consists of a string of even length.
  • The output is a string.

Refer sample output for formatting specifications

Sample Input 1:

this

Sample Output 1:

hi
Sample Input 1:

Hell

Sample Output 1:

el

Fetch Middle Characters from String in Java

Following are the steps to find the middle character from string in Java:

  • Input a string from the user.
  • Pass the string to getMiddleChars() method of UserMainCode class.
  • Create an empty string buffer.
  • Check if the length is even, then fetch the middle characters using the subString() method.
  • At last, just append to the string buffer and return.
import java.util.Scanner;


public class Main4 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    
    String s1 = UserMainCode.getMiddleChars(s);
    System.out.println(s1);
  }
}

 class UserMainCode {
    public static String getMiddleChars(String str)
    {
                StringBuffer sb=new StringBuffer();
                 if(str.length()%2==0)
                 {
                 sb.append(str.substring((str.length()/2)-1,(str.length()/2)+1));
                 }
                 return sb.toString();
                 }
    }

Output

Middle Character of a String using if-else in Java

Following are the steps to print the middle characters of a string in Java:

  • Input a string from the user.
  • Pass the string to getMiddleChars() method of UserMainCode class.
  • Declare and initialize variable index and length.
  • Now, just check if the length is even, fetch the middle character. and if the length is odd, just fetch the middle two characters.
  • At last, pass the substring with both (index) and (index+length).
package com.company;

import java.util.Scanner;

public class Main4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String s1 = UserMainCode.getMiddleChars(s);
        System.out.println(s1);
    }
}

class UserMainCode {
    public static String getMiddleChars(String str) {
       int index, length;
       if(str.length()%2==0){
           index = str.length() / 2 -1;
           length = 2;
       }
       else{
           index = str.length() / 2;
           length = 1;
       }
       return str.substring(index,index+length);
    }
}

Output

Get the SubString from a given string in Java

Obtain a string from the user and get the substring of a given string between two specified positions.

Input and Output Format

  • Input consists of a sentence.
  • The output is a string.

Refer sample output for formatting specifications

Sample Input 1:

Be yourself and quit trying to be everyone else

Sample Output 1:

quit trying to

Program to find the substringĀ  from given string between two specified position

Following are the steps to get the substring of a given string between two specified positions:

  • Input a string from the user.
  • Extract substring using subString() method by passing two positions.
  • At last, just print the string.
package com.company;

import java.util.Scanner;
import java.util.regex.Pattern;

public class Main4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String sentence = scanner.nextLine();
        String str = sentence.substring(16,30);

        System.out.println(str);
    }
}

Output

Thus, in this way, we can find the middle characters from a string. Also, learned how to get the substring from a string between two specified positions.