wap in Java to input a string and check if it's a three-word sentence, outputs the middle word. otherwise, outputs a suitable error message
Share
wap in Java to input a string and check if it's a three-word sentence, outputs the middle word. otherwise, outputs a suitable error message
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Verified answer
Required Answer:-
Question:
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 string: ");
String s=sc.nextLine();
String a[]=s.split(" ");
if(a.length==3) {
System.out.println("String has three words.");
System.out.println("Middle Word is: "+a[1]);
}
else
System.out.println("Error.");
sc.close();
}
}
Explanation:
Output is attached.