Write a program if length of string is even, the reverse the string: MIDDLE LETTER/Reverse first half of the string if string length is ODD


Program
import java.util.*; public class MidLetter { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); int len=str.length(); if(len%2==0) { StringBuilder sb=new StringBuilder(str); sb.reverse(); System.out.println(sb.toString()); } else if(len%2!=0) { System.out.println("Length of string is " +len); int mid=len/2; mid++; System.out.println("Substring of string is "+mid); String substr=str.substring(0,mid-1); String remainSub=str.substring(mid-1,len); StringBuilder sb=new StringBuilder(substr); sb.reverse(); System.out.println(sb.toString()+remainSub); } } }
Input
Output