Write a Java program to input a sentence from the user and display the words which contain the
alphabet 'A' in them in either Lowercase or Uppercase.
Sample Input : All is well that ends well.
Sample Ouptut : All
that
Answers
Answered by
1
Required Answer:-
Question:
- Write a Java program to input a sentence and display the words which contain the alphabet 'A' in them in either Lowercase or Uppercase.
Solution:
Here is the program.
import java.util.*;
public class StringOp {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s=sc.nextLine();
String a[]=s.split(" ");
System.out.println("Word containing letter a are: ");
for(int i=0;i<a.length;i++) {
if(a[i].contains("A")||a[i].contains("a"))
System.out.println(a[i]);
}
sc.close();
}
}
Explanation:
- We have asked the user to enter a sentence. Using string.split() function, we will split the words and store them in array. Now, using contains() function, we will check if the letter A is present in the word. If it's present, then we will print them.
Output is attached.
Attachments:
Similar questions