In this tutorial, you will learn how to get middle character from the word. Here is an example of getting the middle letter of the word entered by the user using Java. If you have a word string 'company' and you want to get the character 'p' from the word, then you can use the below code.
Example:
import java.util.*;
class GetMiddleCharacter
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter word: ");
String str=input.nextLine();
int len=str.length();
int c=len/2;
char ch[]=str.toCharArray();
System.out.println("Middle Character is: "+ch[c]);
}
}
Output:
Enter word: company Middle Character is: p
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.