Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named find Middle Letter that is passed a String parameter and th

ID: 3690793 • Letter: W

Question

Write a method named find Middle Letter that is passed a String parameter and that returns the middle letter of the string. If the length of the parameter is odd then the middle letter is returned For example, "e" is returned if the parameter is "Roberta". If the length of the parameter is even then the letter just before the middle of the string is returned. For example, "b" is returned if the parameter is "Robert". You are guaranteed as a precondition that the parameter will have a length greater than I. This method can be completed without using an i f statement but you may use one if you would like to. public static String find Middle Letter (String word)

Explanation / Answer

I am giving the code with comments to explain the working of code:

import java.util.Scanner;

   class words

   {

      public static String findMiddleletter(String word)

      {

//for even words//

         if ((word.length() % 2) == 0 && (word.length() > 2))

         {

        

              

               return word.substring ( word.length() / 2 - 1, word.length() / 2   );

            }

//For odd words//

             return word.substring(word.length() / 2, word.length() / 2 + 1 );

     

      }

      public static void main(String[] args)

      {

         Scanner in = new Scanner(System.in);

         System.out.print("Enter a word:");

         String word = in.next();

         System.out.println("Middle letter of the word is = " + findMiddleletter(word));

      }

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote