Program to convert first character uppercase in a sentence
Write a Java program to convert first character uppercase in a sentence and if apart from first character if any character is in Uppercase then convert in into Lowercase?
Examples:
Input : gEEKs Output :Geeks Input :GFG Output :Gfg
Method 1:
Java
// Java program to convert first character // uppercase in a sentence class GFG { static String convert(String str) { // Create a char array of given String char ch[] = str.toCharArray(); for (int i = 0; i < str.length(); i++) { // If first character of a word is found if (i == 0 && ch[i] != ' ' || ch[i] != ' ' && ch[i - 1] == ' ') { // If it is in lower-case if (ch[i] >= 'a' && ch[i] <= 'z') { // Convert into Upper-case ch[i] = (char)(ch[i] - 'a' + 'A'); } } // If apart from first character // Any one is in Upper-case else if (ch[i] >= 'A' && ch[i] <= 'Z') // Convert into Lower-Case ch[i] = (char)(ch[i] + 'a' - 'A'); } // Convert the char array to equivalent String String st = new String(ch); return st; } public static void main(String[] args) { String str = "gEEks fOr GeeKs"; System.out.println(convert(str)); } } |
C#
// C# program to convert first character // uppercase in a sentence using System; class GFG { static String convert(String str) { // Create a char array of // given String char []ch = str.ToCharArray(); for (int i = 0; i < str.Length; i++) { // If first character of a // word is found if (i == 0 && ch[i] != ' ' || ch[i] != ' ' && ch[i - 1] == ' ') { // If it is in lower-case if (ch[i] >= 'a' && ch[i] <= 'z') { // Convert into Upper-case ch[i] = (char)(ch[i] - 'a' + 'A'); } } // If apart from first character // Any one is in Upper-case else if (ch[i] >= 'A' && ch[i] <= 'Z') // Convert into Lower-Case ch[i] = (char)(ch[i] + 'a' - 'A'); } // Convert the char array to // equivalent String String st = new String(ch); return st; } // Driver code public static void Main() { String str = "gEEks fOr GeeKs"; Console.Write(convert(str)); } } // This code is contributed by Nitin Mittal. |
Output:
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:
// Java Program to capitalize each word in string public class GFG { // Method to convert the string static String capitailizeWord(String str) { StringBuffer s = new StringBuffer(); // Declare a character of space // To identify that the next character is the starting // of a new word char ch = ' '; for (int i = 0; i < str.length(); i++) { // If previous character is space and current // character is not space then it shows that // current letter is the starting of the word if (ch == ' ' && str.charAt(i) != ' ') s.append(Character.toUpperCase(str.charAt(i))); else s.append(str.charAt(i)); ch = str.charAt(i); } // Return the string with trimming return s.toString().trim(); } // Driver Code public static void main(String args[]) { // Declare the string String s1 = "gEEks fOr GeeKs"; // Convert that string into lowercase s1 = s1.toLowerCase(); // Call the method to capitalize each word System.out.println(capitailizeWord(s1)); } } |
Recommended Posts:
- Java Program to convert Character Array to IntStream
- Convert a String to Character array in Java
- Java program to count the characters in each word in a given sentence
- Java program to swap first and last characters of words in a sentence
- Program to print ASCII Value of a character
- Java Program to get a character from a String
- Java program to count the occurrences of each character
- Program to Convert Radian to Degree
- Program to convert Array to Set in Java
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
- Program to convert a Map to a Stream in Java
- Java program to count the occurrence of each character in a string using Hashmap
- Program to Convert Stream to an Array in Java
- Program to Convert List to Stream in Java
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.

