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.
Input: str = “Java”
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”
The length of the given string is odd.
Therefore, there would be only one middle character, we print that middle character.
- 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:
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.
- String formed with middle character of every right substring followed by left sequentially
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Lexicographically middle string
- Reverse middle words of a string
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Count of substrings having the most frequent character in the string as first character
- Print the string by ignoring alternate occurrences of any character
- Print last character of each word in a string
- Print the first and last character of each word in a String
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Print the string after the specified character has occurred given no. of times
- Print Kth character in sorted concatenated substrings of a string
- Swap corner words and reverse middle characters
- Sum of middle row and column in Matrix
- Program to find GCD or HCF of two numbers using Middle School Procedure
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.