Write a Java program to convert first character uppercase in a sentence and if apart from the first character if any other character is in Uppercase then convert into Lowercase?
Input : gEEKs Output :Geeks Input :GFG Output :Gfg Input : GeeksforGeeks Output : Geeksforgeeks
Geeks For Geeks
Method 2 : Using Java Inbuilt methods
To capitalize each word, call the
toLowerCase() method to put the string in lowercase format. Iterate the string, if any space if found in previous iteration and current element is not space then call the
toUpperCase() method to put the first letter of word in uppercase format and append the string in buffer.
Below is the implementation:
Method 3 : Using Python3
Approach : We can create a list of all the words in a sentence (say string_array). After creating string_array, if we directly work upon that list and try to change the lowercase starting characters of words, we will get an error ” ‘str’ object does not support item assignment“. To avoid this we can create a list of ASCII values (say ascii_array) of characters corresponding to the characters in string_array.
We can easily work upon ascii_array and convert the starting lowercase characters of words to uppercase by simply subtracting 32 from their ASCII value. After changing the values in ascii_array, we convert each ASCII value in ascii_array to their corresponding characters by using chr() function. chr() function returns the character corresponding to the ASCII value passed in parameters. This way we will obtain a list of capitalized strings.
Below is the implementation of the above approach
Geeks For Geeks
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java program to swap first and last characters of words in a sentence
- Move all Uppercase char to the end of string
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
- Java Program to convert Character Array to IntStream
- Java program to count the characters in each word in a given sentence
- Find the count of M character words which have at least one character repeated
- Convert a String to Character array in Java
- Convert Character Array To String In Java
- Capitalize the first and last character of each word in a string
- Queries to find the first non-repeating character in the sub-string of a string
- How to find the first and last character of a string in Java
- Remove first and last character of a string in Java
- Swap the first and last character of a string in Java
- Remove duplicate words from Sentence using Regular Expression
- Hello World Program : First program while learning Programming
- Program to print ASCII Value of a character
- Java program to count the occurrences of each character
- Java Program to get a character from a String
- Java program to count the occurrence of each character in a string using Hashmap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.