Java program to accept a String and Display it in Title Case.
(eq- My name is covid----- My Name Is Covid)
import java.util.Scanner;
public class demo{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String: ");
StringBuffer sb = new StringBuffer(sc.nextLine());
sc.close();
if(Character.isLowerCase(sb.charAt(0)))
sb.replace(0,1,String.valueOf((char)(sb.charAt(0)-32)));
for(int i=1;i<sb.length();i++){
if(sb.charAt(i) == ' ' && Character.isLowerCase(sb.charAt(i+1)))
sb.replace(i+1,i+2,String.valueOf((char)(sb.charAt(i+1)-32)));
}
System.out.println(sb);
}
}
Comments
Post a Comment