Q Write a program to accept any
name and convert it to ProperCase.
Example: Input AMITABH
BANERJEE , OUTPUT Amithabh Banerjee
SOLUTION:
import java.lang.StringBuffer;
import java.lang.String;
import java.util.Scanner;
class q6
{
public static void main(String args[])
{
StringBuffer sb ;
String temp;
char c;
int i = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Sentence :");
temp = sc.nextLine(); //taking input in String class
temp = temp.trim();
temp = temp.toLowerCase();
sb = new StringBuffer(temp); // String -> StringBuffer
sb.insert(i,String.valueOf((char)(sb.charAt(i) - 32)));
i = i + 1;
sb.deleteCharAt(i);
i = i +1;
while(i<sb.length())
{
if(sb.charAt(i) == ' ')
{
i = i + 1 ;
sb.insert(i,String.valueOf((char)(sb.charAt(i) - 32)));
i = i + 1;
sb.deleteCharAt(i);
}
i = i + 1;
}
System.out.println(sb);
}
}
OUTPUT:
Comments
Post a Comment