Java program to make the first letter of a String capital.


import
 java.util.Scanner;
import java.lang.Character;

public class stringUpperCase{

    public static void main(String[] arg
    {
        //Creating Object for Scanner Class
        Scanner sc =  new Scanner(System.in);
        
        //Ask for User Input String and store in a String 'Str'
        System.out.print("Enter String: ");
        String Str = sc.nextLine();
        
        //Adding blank space at the begining of the input String 
//and Store in a new String 'Str1'
        String Str1 = " "+Str;
                
        //Coverting String into a Character Array
        char[] ch = Str1.toCharArray();

        //Checking for space in character array 

        for(int i=0;i<ch.length;i++)
        {
            //Converting the character next to the space into UPPERCASE 
//and store at the same index position
            if(ch[i]==' ')
            
            ch[i+1]= Character.toUpperCase(ch[i+1]);
            
        }

        //Coverting the character Array into String and Store in a new String 'mssg'
        String mssg = new String(ch);
    
        //Printing the final String
                System.out.print(mssg);
        
    }
}

Comments

Popular posts from this blog

Java Program to replace vowel with next immediate character alphabetically in a String.

Java Program to count and output the number of double letter sequences that exist in the string.

Java Program to find the minimum and maximum palindrome String in a Sentence