Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
First and last character of each word in a String
Next article icon

Print the middle character of a string

Last Updated : 10 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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<bits/stdc++.h>
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;
 
    # Print the 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
 
 

Javascript




<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>
 
 
Output: 
o

 

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



Next Article
First and last character of each word in a String
author
prashant_srivastava
Improve
Article Tags :
  • DSA
  • School Programming
  • Strings
  • Java-String-Programs
  • strings
Practice Tags :
  • Strings
  • Strings

Similar Reads

  • Print the first and last character of each word in a String
    Given a string s consisting of multiple words, print the first and last character of each word. If a word contains only one character, print it twice (since the first and last characters are the same).Note: The string will not contain leading or trailing spaces. Examples: Input: s = "Geeks for geeks
    5 min read
  • Sort string of characters
    Given a string of lowercase characters from 'a' - 'z'. We need to write a program to print the characters of this string in sorted order. Examples: Input : "dcab" Output : "abcd"Input : "geeksforgeeks"Output : "eeeefggkkorss" Naive Approach - O(n Log n) TimeA simple approach is to use sorting algori
    5 min read
  • Program for removing i-th character from a string
    Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a
    5 min read
  • Convert characters of a string to opposite case
    Given a string, convert the characters of the string into the opposite case,i.e. if a character is the lower case then convert it into upper case and vice-versa. Examples: Input : geeksForgEeksOutput : GEEKSfORGeEKSInput : hello every oneOutput : HELLO EVERY ONEASCII values of alphabets: A - Z = 65
    15+ min read
  • Iterate over characters of a string in C++
    Given a string str of length N, the task is to traverse the string and print all the characters of the given string. Examples: Input: str = "GeeksforGeeks" Output: G e e k s f o r G e e k s Input: str = "Coder" Output: C o d e r Naive Approach: The simplest approach to solve this problem is to itera
    3 min read
  • Find k'th character of decrypted string | Set 1
    Given an encoded string, where repetitions of substrings are represented as substring followed by count of substrings. For example, if encrypted string is "ab2cd2" and k=4 , so output will be 'b' because decrypted string is "ababcdcd" and 4th character is 'b'. Note: Frequency of encrypted substring
    8 min read
  • Program to Search a Character in a String
    Given a character ch and a string s, the task is to find the index of the first occurrence of the character in the string. If the character is not present in the string, return -1. Examples: Input: s = "geeksforgeeks", ch = 'k'Output: 3Explanation: The character 'k' is present at index 3 and 11 in "
    6 min read
  • Print the string by ignoring alternate occurrences of any character
    Given a string of both uppercase and lowercase alphabets, the task is to print the string with alternate occurrences of any character dropped(including space and consider upper and lowercase as same). Examples: Input : It is a long day Dear. Output : It sa longdy ear. Print first I and then ignore n
    4 min read
  • Find last index of a character in a string
    Given a string str and a character x, find last index of x in str. Examples : Input : str = "geeks", x = 'e' Output : 2 Last index of 'e' in "geeks" is: 2 Input : str = "Hello world!", x = 'o' Output : 7 Last index of 'o' is: 7 Recommended PracticeLast index of a character in the stringTry It! Metho
    8 min read
  • Print consecutive characters together in a line
    Given a sequence of characters, print consecutive sequence of characters in a line, otherwise print it in a new line. Examples: Input : ABCXYZACCD Output : ABC XYZ A C CD Input : ABCZYXACCD Output: ABC ZYX A C CD The idea is to traverse string from left to right. For every traversed character, print
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences