Question

Modify the code (that recognizes words ending with ing) to a code that recognizes the keywords...

Modify the code (that recognizes words ending with ing) to a code that recognizes the keywords if, then and else. In case it recognizes if it should output KEYWORD_IF,… If the word is not recognized, it should return an ERROR message.
Scanner scan = new Scanner(System.in);
String s = scan.next();
int q = 0;
for (char c : s.toCharArray()) {
switch (q) {
case 0: q = (c=='i')? 1 : 0; break;
case 1: q = (c=='n')? 2 : ((c=='i')? 1 : 0); break;
case 2: q = (c=='g')? 3 : ((c=='i')? 1 : 0); break;
case 3: q = (c=='i')? 1 : 0;
}
}
if (q==3)
System.out.println("accept.");
else
System.out.println("reject.");

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot:

Code:

import java.io.*;
import java.util.*;

public class Main
{
   public static void main(String[] args) {
        //Scanner Object for user input
        Scanner scan=new Scanner(System.in);
        String s = scan.next();
         int n=s.length();
         //if word length is greater than or equal to 3 and end with ing then word is accept else rejected
            if (n>=3 && s.charAt(n-3)=='i'&& s.charAt(n-2)=='n'&& s.charAt(n-1)=='g')
            System.out.println("accept.");
            else
            System.out.println("reject.");
  
   }
}

Add a comment
Know the answer?
Add Answer to:
Modify the code (that recognizes words ending with ing) to a code that recognizes the keywords...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Modify the program below so it also calculates and displays the amount of money Package A...

    Modify the program below so it also calculates and displays the amount of money Package A customers would save if they purchased Package B or C, and the amount of money Package B customers would save if they purchased Package C. If there would be no savings, no message should be printed. import java.util.*; public class Lab2 { public static void main (String[] args) {    //scanner Scanner input = new Scanner(System.in);    //declarations int hours; char pack; double totalCharges...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • Modify your Rock Paper Scissor (Lizard/Spock) game from Week 5 to generate a random number for...

    Modify your Rock Paper Scissor (Lizard/Spock) game from Week 5 to generate a random number for Player 2 and assign that as Player 2's choice. The rest of your program should stay the same. You can use your program with the nested if statements or the switch statement. The implementation should use the Random class to get your random number. The assignment is worth 10 points. Upload your submission to the assignment. code: import java.util.Scanner; public class RPSLSYourLastName {   ...

  • Let's fix the displayMenu() method! First, edit the method to do the following: We want to...

    Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now?                  ...

  • The files provided contain syntax and/or logic errors. In each case, determine and fix the problem,...

    The files provided contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. import java.util.*; public class DebugEight1 { public static void main(String args[]) { Scanner input = new Scanner(System.in); char userCode; String entry, message; boolean found = false; char[] okayCodes = {'A''C''T''H'}; StringBuffer prompt = new StringBuffer("Enter shipping code for this delivery\nValid codes are: "); for(int x = 0; x <...

  • Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words)...

    Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words) the logic of the program and how the code can be   optimized. Modify the code to repeat the program until the user enters “no”. (*I need the logical write out on how the program behaves when there is an input from the user. (I don't need the //comments). import java.util.Scanner; public class Q2 {     public static void main(String[] args)     {         Scanner...

  • For this code after case 0, I need it to ask the user if they would...

    For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program. import java.util.InputMismatchException; import java.util.*; import java.io.*;...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT