Write a program to accept a sentence and check whether it is terminated by ‘.’ or ‘? ‘ or ‘!’ . Display the words using default delimiter (space) and the first character of each word should be converted into Upper Case.
Sample Input :
converting first character.
Sample Output:
converting Converting
first First
character Character
import java.util.*;
class strDec
{
public static String firstLetter(String wrd)
{
String res="";char ch2=' ';
int l=wrd.length();
for(int i=1;i<l;i++)
{
char ch1=wrd.charAt(i);
ch2=wrd.charAt(0);
if(Character.isLowerCase(ch2))
{
ch2 =Character.toUpperCase(ch2);
}
//System.out.println(ch1);
res=res +ch1;
}
res=ch2+res;
return res;
//System.out.println(res);
}
public static void main(String args[])
{ strDec strd=new strDec();
String str;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
str=sc.nextLine();
int l=str.length();
char c=str.charAt(l-1);
if(c=='.'||c=='?'||c=='!')
{
StringTokenizer st=new StringTokenizer( str, " .?!");
int n=st.countTokens();
String wrd[]=new String [n];
String ch[]=new String[n];
for(int i=0;i<n;i++){
wrd[i]=st.nextToken();
ch[i]=firstLetter(wrd[i]);
System.out.println(wrd[i] + " "+ch[i]+" " );
}
}
else
{
System.out.println("Enter a sentence terminated with . or ? or ! :");
}
}
}
Output:
Enter a sentence:
the sky is the limit.
the The
sky Sky
is Is
the The
limit Limit