How to resolve the algorithm Odd word problem step by step in the Java programming language
How to resolve the algorithm Odd word problem step by step in the Java programming language
Table of Contents
Problem Statement
Write a program that solves the odd word problem with the restrictions given below.
You are promised an input stream consisting of English letters and punctuations.
It is guaranteed that:
A stream with six words:
The task is to reverse the letters in every other word while leaving punctuations intact, producing: while observing the following restrictions:
Work on both the "life" example given above, and also the text:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Odd word problem step by step in the Java programming language
Purpose: "Oddword" is a Java program that reads a stream of characters from standard input and prints them in a modified way: letters are displayed in reverse order while non-letters are displayed in their original order.
Implementation Details:
-
CharHandler
Interface:- Defines a functional interface that represents an operation to handle a character.
- The
handle
method takes a character as input and returns a newCharHandler
to continue processing.
-
fwd
CharHandler:- Implements the
CharHandler
interface for handling forward processing. - Prints the character and returns itself for subsequent characters.
- If the character is a letter, it switches to the
rev
CharHandler for reverse processing.
- Implements the
-
Reverser
Class:- Extends
Thread
and implements theCharHandler
interface for reverse processing. - Uses inter-thread communication to store the character for reverse processing.
- In its
run
method, it continuously waits for a character, prints it, and waits for the character to be consumed. - In its
handle
method, it stores the received character and returns itself for subsequent characters. - If the character is a letter, it continues reverse processing by calling the
recur
method.
- Extends
-
rev
CharHandler:- An instance of the
Reverser
class that handles reverse processing.
- An instance of the
-
loop
Method:- Starts the
Reverser
thread. - Reads characters from standard input and calls the
handle
method of the currentCharHandler
to process each character.
- Starts the
-
main
Method:- Creates an instance of
OddWord
and calls itsloop
method to process the input.
- Creates an instance of
Execution Flow:
- Characters are read from the standard input.
- The
fwd
CharHandler handles non-letters by printing them directly. - For letters, it switches to the
rev
CharHandler, which starts theReverser
thread for reverse processing. - The
Reverser
thread waits for characters to be processed in reverse order. - When a letter is encountered, it switches back to the
fwd
CharHandler for forward processing. - This process continues until the end of the input.
- As a result, letters are printed in reverse order while non-letters are printed in their original order.
Source code in the java programming language
public class OddWord {
interface CharHandler {
CharHandler handle(char c) throws Exception;
}
final CharHandler fwd = new CharHandler() {
public CharHandler handle(char c) {
System.out.print(c);
return (Character.isLetter(c) ? fwd : rev);
}
};
class Reverser extends Thread implements CharHandler {
Reverser() {
setDaemon(true);
start();
}
private Character ch; // For inter-thread comms
private char recur() throws Exception {
notify();
while (ch == null) wait();
char c = ch, ret = c;
ch = null;
if (Character.isLetter(c)) {
ret = recur();
System.out.print(c);
}
return ret;
}
public synchronized void run() {
try {
while (true) {
System.out.print(recur());
notify();
}
} catch (Exception e) {}
}
public synchronized CharHandler handle(char c) throws Exception {
while (ch != null) wait();
ch = c;
notify();
while (ch != null) wait();
return (Character.isLetter(c) ? rev : fwd);
}
}
final CharHandler rev = new Reverser();
public void loop() throws Exception {
CharHandler handler = fwd;
int c;
while ((c = System.in.read()) >= 0) {
handler = handler.handle((char) c);
}
}
public static void main(String[] args) throws Exception {
new OddWord().loop();
}
}
You may also check:How to resolve the algorithm Exceptions step by step in the C++ programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Java programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Pascal programming language
You may also check:How to resolve the algorithm Cyclotomic polynomial step by step in the Phix programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Quackery programming language