In: Computer Science
• Develop an "Echo" command that returns the parameters on
different lines.
Example: For input
"$ Java Echo Hello world!"
The answer will be:
"Hello
world
! ”
using java programming language
Every line of the code is clearly explained in the comments of the code itself.
CODE--
import java.util.*;
public class Echo
{
public static void main(String[] args)
{
Scanner sc=new
Scanner(System.in);
String cmd;
//read the command from the
user
cmd=sc.nextLine();
//store each word in a separate
array variable
String[] words=cmd.split("[!.
?\"]");
//Echo or display the given
output
System.out.print("\"");
//we start from index 4
because
//the indices before 4 contain the
command
//which is not required to be
displayed
for(int
i=4;i<words.length;i++)
{
System.out.println(words[i]);
}
//here we check if there is any
punctuation at the end
if(cmd.charAt(cmd.length()-2)=='!'
||cmd.charAt(cmd.length()-2)=='.'
||cmd.charAt(cmd.length()-2)=='?')
{
//if so we
display the punctuation
System.out.println(cmd.charAt(cmd.length()-2)+"\"");
}
sc.close();
}
}
Output Screenshot:
Please upvote if you liked the effort.