# Print the middle character of a string

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:

## C++

 `// C++ program to implement` `// the above approach` `#include` `using` `namespace` `std;`   `// Function that prints the middle` `// character of a string` `void`  `printMiddleCharacter(string str)` `{` `    ``// Finding string length` `    ``int` `len = str.size();`   `    ``// Finding middle index of string` `    ``int` `middle = len / 2;`   `    ``// Print the middle character` `    ``// of the string` `    ``cout << str[middle];` `}`   `// Driver Code` `int` `main()` `{` `    ``// Given string str` `    ``string str = ``"GeeksForGeeks"``;`   `    ``// Function Call` `    ``printMiddleCharacter(str);` `    ``return` `0;` `}`   `// This code is contributed by Sapnasingh`

## Java

 `// Java program for the above approach` `class` `GFG {`   `    ``// Function that prints the middle` `    ``// character of a string` `    ``public` `static` `void` `    ``printMiddleCharacter(String str)` `    ``{` `        ``// Finding string length` `        ``int` `len = str.length();`   `        ``// Finding middle index of string` `        ``int` `middle = len / ``2``;`   `        ``// Print the middle character` `        ``// of the string` `        ``System.out.println(str.charAt(middle));` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `    ``main(String args[])` `    ``{` `        ``// Given string str` `        ``String str = ``"GeeksForGeeks"``;`   `        ``// Function Call` `        ``printMiddleCharacter(str);` `    ``}` `}`

## Python3

 `# Python3 program for the above approach`   `# Function that prints the middle` `# character of a string` `def` `printMiddleCharacter(``str``):` `    `  `    ``# Finding string length` `    ``length ``=` `len``(``str``);`   `    ``# Finding middle index of string` `    ``middle ``=` `length ``/``/` `2``;`   `    ``# Prthe middle character` `    ``# of the string` `    ``print``(``str``[middle]);`   `# Driver Code`   `# Given string str` `str` `=` `"GeeksForGeeks"``;`   `# Function Call` `printMiddleCharacter(``str``);`   `# This code is contributed by sapnasingh4991`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG{`   `// Function that prints the middle` `// character of a string` `public` `static` `void` `printMiddlechar(String str)` `{` `    `  `    ``// Finding string length` `    ``int` `len = str.Length;`   `    ``// Finding middle index of string` `    ``int` `middle = len / 2;`   `    ``// Print the middle character` `    ``// of the string` `    ``Console.WriteLine(str[middle]);` `}`   `// Driver Code` `public` `static` `void` `Main(String []args)` `{` `    `  `    ``// Given string str` `    ``String str = ``"GeeksForGeeks"``;`   `    ``// Function call` `    ``printMiddlechar(str);` `}` `}`   `// This code is contributed by amal kumar choubey`

Output:

```o

```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.