Print the middle character of a string

10 Nov, 2021
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: vExplanation: 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: oExplanation: The length of the given string is odd. Therefore, there would be only one middle character, we print that middle character.
Approach:
Get the string whose middle character is to be found.Calculate the length of the given string.Finding the middle index of the string.Now, print the middle character of the string at index middle using function charAt() in Java.
Get the string whose middle character is to be found.
Calculate the length of the given string.
Finding the middle index of the string.
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++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach#include<bits/stdc++.h>using namespace std; // Function that prints the middle// character of a stringvoid  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 Codeint main(){    // Given string str    string str = "GeeksForGeeks";     // Function Call    printMiddleCharacter(str);    return 0;} // This code is contributed by Sapnasingh
// Java program for the above approachclass 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 program for the above approach # Function that prints the middle# character of a stringdef printMiddleCharacter(str):         # Finding string length    length = len(str);     # Finding middle index of string    middle = length // 2;     # Print the middle character    # of the string    print(str[middle]); # Driver Code # Given string strstr = "GeeksForGeeks"; # Function CallprintMiddleCharacter(str); # This code is contributed by sapnasingh4991
// C# program for the above approachusing System; class GFG{ // Function that prints the middle// character of a stringpublic 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 Codepublic static void Main(String []args){         // Given string str    String str = "GeeksForGeeks";     // Function call    printMiddlechar(str);}} // This code is contributed by amal kumar choubey
<script>    // Javascript program for the above approach         // Function that prints the middle    // character of a string    function printMiddleCharacter(str)    {        // Finding string length        let len = str.length;          // Finding middle index of string        let middle = parseInt(len / 2, 10);          // Print the middle character        // of the string        document.write(str[middle]);    }         // Given string str    let str = "GeeksForGeeks";     // Function Call    printMiddleCharacter(str); // This code is contributed by divyeshrabadiya07.</script>
o
 
Time Complexity: O(1) Auxiliary Space: O(1) 
Amal Kumar Choubey
sapnasingh4991
divyeshrabadiya07
Kirti_Mangal
Java-String-Programs
strings
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Exception Handling in C++
Different ways of Reading a text file in Java
Operators in C / C++
Introduction To PYTHON
Destructors in C++
Write a program to reverse an array or string
KMP Algorithm for Pattern Searching
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Python program to check if a string is palindrome or not