You are on page 1of 2

Blue Ridge Public School

Class X – Computer Applications

A.Y. 2023-24

ASSIGNMENT – 17

1. Write a program to accept a sentence from the user and convert the
first character of every word to uppercase.

Sample Input: I like to read


Sample Output: I Like To Read

import java.util.*;
public class StringCaseChange
{
public void stringConvert(String s)
{
s = " " + s;
String cs = "";
char ch;
for(int i = 0; i < s.length(); i++)
{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
{
ch = Character.toUpperCase(s.charAt(i+1));
if(i!= 0)
cs = cs + " " + ch;
else
cs = cs + ch;
i++;
}
else
cs += ch;
}
System.out.println("The original string is: " +
s.trim());

System.out.println("The converted string is: " +


cs.trim());

public static void main(String [] args)


{
StringCaseChange obj = new StringCaseChange();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
obj.stringConvert(sc.nextLine());
}
}
OUTPUT

Enter a sentence:
We are in a cyber world
The original string is: We are in a cyber world
The converted string is: We Are In A Cyber World

Variable Name Data type Description

s, cs String stores String values

ch char stores a character value

i int stores an integer value

You might also like