Q. Write a program to accept a sentence and then convert it to Title Case.
e.g. INPUT: HONESTY IS BEST POLICY
OUTPUT: Honesty Is Best Policy
SOLUTION:
import java.lang.StringBuffer;
import java.lang.String;
import java.util.Scanner;
class q2
{
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);
}
}
Comments
Post a Comment