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


Comments

Post a Comment

Popular posts from this blog

Reasoning-Number Series

Reasoning-Letter Series

Profit and Loss