Menu Close

First half of the string – Java program …ftc

Write a Java program for, Given a string of even length, return the first half.

Input:
DrWhoHoo

Output:
DrW
import java.io.*;
import java.util.*;
public class temp{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter any String:  ");
    String str = scan.nextLine();

    for(int i=0;i<str.length()/2;i++){
      System.out.print(str.charAt(i));
    }

  }
}

INPUT_1:
Enter any String:  Deadpool

OUTPUT:
Dead


INPUT_2:
Enter any String:  fcukthecode.co

OUTPUT:
fcukthe


INPUT_3:
Enter any String:  OnePiece

OUTPUT:
OneP


INPUT_4:
Enter any String:  MetaverseWorld

OUTPUT:
Metaver


INPUT_5:
Enter any String:  123456

OUTPUT:
123


INPUT_6:
Enter any String:  tokyoghoul

OUTPUT:
tokyo


ILLUSTRATION

Executed using javac on linux terminal

Leave a Reply

Your email address will not be published. Required fields are marked *