Write a Java method to display the middle character of a string.
import java.util.Scanner;
public class MiddleLetter{
public static void getMiddleCharacter(String value){
int length=value.length();
int remainder=0;
if(length%2==0){
remainder=length/2;
int priorLetter=remainder-1;
System.out.println(String.valueOf(value.charAt(priorLetter))+String.valueOf(value.charAt(remainder)));
}else{
remainder=length/2;
System.out.println(value.charAt(remainder));
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String word=sc.next();
getMiddleCharacter(word);
sc.close();
}
}
Output:
Enter the string
umapathi
pa
Ntumeclampo_1993 Donna Bonham https://wakelet.com/wake/wnbfDiZuvx2pujZIcLFjz
ReplyDeletecisemira
AbapbelVriota-1998 Mike Flynn Your Uninstaller
ReplyDeleteFirefox browser
Bootstrap Studio
roepesigod