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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right




My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

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.





Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.