Question
Program to print words whose first character of the word is uppercase and last character of the word is lowercase.
Enter the Sentence:
Quick brown fox Jumped over the Lazy dog
Words whose first letter is uppercase and last letter is lowerCase:
Quick Jumped Lazy
Enter the Sentence:
The capital of India is New Delhi.
Words whose first letter is uppercase and last letter is lowerCase:
The India New Delhi
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class WordWithFirstLetterUppercaseAndLastLetterLowercase
{
public static void main()
{
int i=0,len=0,lengthOfWord=0;
String sen="",newSen="",wd="";
char ch=' ',lastCharacter=' ';
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence that ends with a full stop:");
sen=sc.nextLine();
len=sen.length();
lastCharacter=sen.charAt(len-1);
//This 'if' condition will put full stop if user has not done it
if(lastCharacter!='.')
{
sen=sen+".";
len++;
}
for(i=0;i< len;i++)
{
ch=sen.charAt(i);
if(ch==' '||ch=='.')
{
lengthOfWord=wd.length();
lastCharacter=wd.charAt(lengthOfWord-1);
// This 'if' condition case check if the first character is uppercase and last character is lowercase.
if(Character.isUpperCase(wd.charAt(0))&&Character.isLowerCase(lastCharacter)==true)
{
newSen=newSen+wd;
newSen=newSen+" ";
}
wd="";
}
else
{
wd=wd+ch;
}
}
System.out.println("Words whose first letter is uppercase and last letter is lowerCase:");
System.out.println(newSen);
}
}
Coding Store
Sale
ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale