Java program to accept a word and print the first letter of each word Eg: This is a program Output : TIAP
Answers
Answered by
18
Heya friend,
The logic is to store every letter after " "[ <blankspace>||whitespace
hence adding a space to the INPUT sentence ]
We use charAt() function to get each character of the input sentence
and then we check for the space using the if-statement
then storing the letter
program as follows
import java.io.*;
class FirstLetter
{
public static void main(String args[])throws IOException
{
DataInputStream ak = new DataInputStream(System.in);
String str;
char y;
int lengt;
System.out.print("Enter any sentence: ");
str = ak.readLine();
//adding a space in front of the input sentence
str = " " + str;
str = str.toUpperCase();
//finding the length of the input sentence
lengt = str.length();
System.out.print("Output = ");
for(int i=0;i<lengt;i++)
{
//taking out one character at a time from the sentence
y = str.charAt(i);
if(y==' ')
{
System.out.print(str.charAt(i+1)+".");
}
}
}
}
HOPE THIS HELPS!!!!!!!!
The logic is to store every letter after " "[ <blankspace>||whitespace
hence adding a space to the INPUT sentence ]
We use charAt() function to get each character of the input sentence
and then we check for the space using the if-statement
then storing the letter
program as follows
import java.io.*;
class FirstLetter
{
public static void main(String args[])throws IOException
{
DataInputStream ak = new DataInputStream(System.in);
String str;
char y;
int lengt;
System.out.print("Enter any sentence: ");
str = ak.readLine();
//adding a space in front of the input sentence
str = " " + str;
str = str.toUpperCase();
//finding the length of the input sentence
lengt = str.length();
System.out.print("Output = ");
for(int i=0;i<lengt;i++)
{
//taking out one character at a time from the sentence
y = str.charAt(i);
if(y==' ')
{
System.out.print(str.charAt(i+1)+".");
}
}
}
}
HOPE THIS HELPS!!!!!!!!
Answered by
2
ANSWER
..........................
import java.util.*;
public class First Letter Capital
{
public
static
void main
(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the sentence.");
String s=ob.nextLine();
s=" "+s;
String cap="";
for(int i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x==' ')
{
cap=cap+" ";
char y=s.charAt(i+1);
cap=cap+Character .to UPPERCASE (y);
i++;
}
else
{
cap=cap+x;
}
}
System.out.println("The new String with capital letters is: "+"\n"+cap);
}
}
..........................
import java.util.*;
public class First Letter Capital
{
public
static
void main
(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the sentence.");
String s=ob.nextLine();
s=" "+s;
String cap="";
for(int i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x==' ')
{
cap=cap+" ";
char y=s.charAt(i+1);
cap=cap+Character .to UPPERCASE (y);
i++;
}
else
{
cap=cap+x;
}
}
System.out.println("The new String with capital letters is: "+"\n"+cap);
}
}
Similar questions