Write a program to accept a sentence and print only the first letter of each word of the sentence in capital letters separated by a full stop in java
Answers
A program to accept a sentence and print only the first letter of each word of the sentence in capital letters separated by a full stop in JAVA is as follows:
import java . io . *;
class ini
{
public static void main ( ) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
String a;
char y;
int d;
System.out.print(" Please enter any sentence : ");
a=br . readLine( );
a=" "+a;
a=a . toUpperCase( );
d=a . length();
System. out. print("Output is = ");
for(int i=0; i<l ;i++)
{
x=a. charAt ( i );
if(x==' ')
System. out. print(a. charAt(i+1)+" . ");
}
}
}
Answer:
import java.io.*;
class Initials
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
char x;
int l;
System.out.print("Enter any sentence: ");
s=br.readLine();
s=" "+s; //adding a space infront of the inputted sentence or a name
s=s.toUpperCase(); //converting the sentence into Upper Case (Capital Letters)
l=s.length(); //finding the length of the sentence
System.out.print("Output = ");
for(int i=0;i<l;i++)
{
x=s.charAt(i); //taking out one character at a time from the sentence
if(x==' ') //if the character is a space, printing the next Character along with a fullstop
System.out.print(s.charAt(i+1)+".");
}
}
}
Explanation:
- We know that the letters of the initials are characters which come immediately after a expanse.
- Using the above logic, we are able to extract all the letters of an initial, except the primary letter, because there's no space ahead of the primary word, and hence the logic mentioned above won’t work for the primary word of the sentence.
- So, after inputting a sentence, we add an area before it, to suit within the above mentioned logic, so as to extract the primary letter of the initials.
- Next we convert the inputted sentence into graphic symbol (Capital letters) because, the characters of the Initials are dead grapheme.
- Then, inside the for loop which runs though the entire length of a sentence, we extract each characters one by one and check whether it's a area or not.
- Finally, if the character may be a expanse, then we print the character after it together with a point.
#SPJ3