VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-the-middle-character-of-a-string/

⇱ Print the middle character of a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print the middle character of a string

Last Updated : 15 Jul, 2025

Given string str, the task is to print the middle character of a string. If the length of the string is even, then there would be two middle characters, we need to print the second middle character.

Examples:

Input: str = "Java"
Output: v
Explanation: 
The length of the given string is even. 
Therefore, there would be two middle characters 'a' and 'v', we print the second middle character.

Input: str = "GeeksForGeeks"
Output: o
Explanation: 
The length of the given string is odd. 
Therefore, there would be only one middle character, we print that middle character.

Approach:

  1. Get the string whose middle character is to be found.
  2. Calculate the length of the given string.
  3. Finding the middle index of the string.
  4. Now, print the middle character of the string at index middle using function charAt() in Java.

Below is the implementation of the above approach:


Output: 
o

 

Time Complexity: O(1) 
Auxiliary Space: O(1)
 

Comment
Article Tags: