Java program to make the first letter of a String capital.
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
Post a Comment