You are on page 1of 3

Blue Ridge Public School

Class X – Computer Applications

A.Y. 2023-24

ASSIGNMENT – 18

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

Sample Input: I like to read


Sample Output: I.Like.To.Read

import java.util.*;
public class StringCaseChange2
{
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) && i==0)
{
ch = Character.toUpperCase(s.charAt(i+1));
cs = cs + ch;
i++;

}
else if(Character.isWhitespace(ch))
{
ch = Character.toUpperCase(s.charAt(i+1));
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)


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

Enter a sentence:
Welcome to java
The original string is: Welcome to java
The converted string is: Welcome.To.Java

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