Solve the following program
Attachments:
Answers
Answered by
1
Question:-
- Write a program in Java to ask the user to enter any sentence and a character. Convert the sentence to uppercase and print only these words of the sentence that starts with that particular character.
- Example :-
Enter a sentence: brainly is a best app!
Enter a character: b
Output:-
BRAINLY
BEST
Approach:-
//Here you go!
import java.util.Scanner;
class Check{
public static void main (String ar[]){
Scanner sc=new Scanner (System.in);
System.out.print("Enter a sentence: ");
String s=sc.nextLine().toUpperCase()+" ";
System.out.print("Enter a character: ");
char ch=Character.toUpperCase(sc.next().charAt(0));
String wd="";
System.out.println("Output:-");
for(int i=0;i<s.length();i++){
if (s.charAt(i)!= ' ')
wd+=s.charAt(i);
else {
if(wd.charAt(0)==ch)
System.out.println(wd);
wd="";
}
}
}
}
Note:-
- You can also simply use startsWith() like "Brainly".startsWith("B")
- >> true
- But, you need to make sure that the parameter is in String data type otherwise it will return error
Attachments:
Similar questions