How to find the reverse of each word in a given sentence ?

This is the complete solution to find the reverse of each word from the given sentence. This solution is designed to to solve the problem using an interface, super class and two derived classes implementing the interface.

package pack1;

public interface ReversingEachWordIntf {

	/* Define the functional interface with SAM 'ParseEachWord' */
	StringBuffer ParseEachWord(String str1);

	/* Define the static method 'displayMsg' */
	static void displayMsg(String inpSen, StringBuffer pniSen) {

		System.out.println("This sentence '" + inpSen + "' after reversing '" + pniSen + "'");
		System.out.println("\n");
	}
}
package pack1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import java.util.stream.Collectors;

class InputSequenceOfStrings {

	private String strParam;

	/* Declare the logger object for the class to display the log messages */
	static Logger logger = Logger.getLogger(InputSequenceOfStrings .class.getName());

	public boolean validateInputs(String inpSentence) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "validateInputs()";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		/* Validating the function parameters using Optional String data type methods */
		Optional<String> strInput = Optional.ofNullable(inpSentence);

		/*
		 * Fetch the value of the Optional String parameters using 'isPresent' condition
		 * check. If the value is available use 'get' method otherwise simply return
		 * 'false' to stop the program execution without proceeding further.
		 */
		if (strInput.isPresent()) {
			setStrParam(strInput.get());
		} else {
			return false;
		}

		logger.info("The provided Input validated Successfully");
		return true;
	}

	/* Define getter and setters for strParam variables */
	public String getStrParam() {
		return strParam;
	}

	public void setStrParam(String strings) {
		this.strParam = strings;
	}
}

class ReversingEachWord_Using_StringBuffer extends InputSequenceOfStrings implements ReversingEachWordIntf {

	public StringBuffer ParseEachWord(String inpSentence) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseEachWord";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		StringBuffer resultStr = new StringBuffer();

		if (validateInputs(inpSentence)) {

			System.out.println("The given input sentence is = '" + inpSentence + "'");
			/* Traversing through each word of the sentence, and then reverse each token */
			StringTokenizer words = new StringTokenizer(inpSentence);
			while (words.hasMoreTokens()) {
				StringBuffer nextWord = new StringBuffer(words.nextToken());
				nextWord.reverse();
				resultStr.append(nextWord + " ");
			}

		} else
			logger.info("The given Input sentence is null");

		ReversingEachWordIntf.displayMsg(inpSentence, resultStr);
		return resultStr;
	}
}

class ReversingEachWord_Using_String extends InputSequenceOfStrings implements ReversingEachWordIntf {

	public StringBuffer ParseEachWord(String inpSentence) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseEachWord";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		String resultStr = new String("");
		if (validateInputs(inpSentence)) {

			System.out.println("The given input sentence is = '" + inpSentence + "'");
			/* Split the input sentence into list of words with space as delimiter */
			List<String> words = Arrays.asList(inpSentence.split("\\s"));

			/*
			 * Pass each word to the reverse function which returns the reversed string
			 * value
			 */
			for (int i = 0; i < words.size(); i++) {
				resultStr = resultStr + ReversingEachWord(words.get(i)) + " ";
			}

		} else
			logger.info("The given Input sentence is null");

		ReversingEachWordIntf.displayMsg(inpSentence, new StringBuffer(resultStr));
		return new StringBuffer(resultStr);
	}

	private String ReversingEachWord(String strVar) {
		/* Using basic logic of storing characters in the reverse order */
		String strRev = "";
		for (int i = strVar.length() - 1; i >= 0; i--)
			strRev = strRev + strVar.charAt(i);
		return strRev;
	}
}

class ReversingEachWord_Using_LambdaExp extends InputSequenceOfStrings implements ReversingEachWordIntf {

	public StringBuffer ParseEachWord(String inpSentence) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseEachWord";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		/* Define the Lambda expression for the functional interface 'ParseEachWord' */
		ReversingEachWordIntf Rew = ((Sentence) -> {
			/*
			 * Process each word in a Sentence, and store in 'resStr' StringBuffer variable
			 */
			StringTokenizer allWords = new StringTokenizer(Sentence);
			StringBuffer resStr = new StringBuffer();
			while (allWords.hasMoreTokens()) {
				StringBuffer nextWord = new StringBuffer(allWords.nextToken());
				resStr.append(nextWord.reverse() + " ");
			}
			return resStr;
		});

		StringBuffer resultStr = new StringBuffer();
		if (validateInputs(inpSentence)) {
			System.out.println("The given input sentence is = '" + inpSentence + "'");
			/* call the lambda expression */
			resultStr = Rew.ParseEachWord(inpSentence);
		} else
			logger.info("The given Input sentence is null");

		ReversingEachWordIntf.displayMsg(inpSentence, resultStr);
		return resultStr;
	}
}

class ReversingEachWord_Using_Stream extends InputSequenceOfStrings implements ReversingEachWordIntf {

	public StringBuffer ParseEachWord(String inpSentence) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "ParseEachWord";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		ArrayList<StringBuffer> arrListStrs = new ArrayList<StringBuffer>();
		if (validateInputs(inpSentence)) {

			System.out.println("The given input sentence is = '" + inpSentence + "'");
			/* Split the input sentence into list of words with space as delimiter */
			String[] arrStrs = inpSentence.split("\\s+");
			for (String str : arrStrs)
				arrListStrs.add(new StringBuffer(str));

			/* Applying map() of stream, and reverse () of string buffer */
			arrListStrs.stream().map(s -> s.reverse()).collect(Collectors.toList());

		} else
			logger.info("The given Input sentence is null");

		ReversingEachWordIntf.displayMsg(inpSentence, new StringBuffer(Arrays.toString(arrListStrs.toArray())));
		return new StringBuffer(Arrays.toString(arrListStrs.toArray()));
	}
}

public class ReversingEachWordJ8 {

	public static void main(String[] args) {

		/*
		 * create the class object ReversingEachWord_Using_StringBuffer, and call the
		 * method ParseAlphanumeric
		 */
		ReversingEachWord_Using_StringBuffer REWUStrB = new ReversingEachWord_Using_StringBuffer();
		REWUStrB.ParseEachWord("Java developers are in demand");
		REWUStrB.ParseEachWord(null);

		/*
		 * create the class object ReversingEachWord_Using_String, and call the method
		 * ParseAlphanumeric
		 */
		ReversingEachWord_Using_String REWUStr = new ReversingEachWord_Using_String();
		REWUStr.ParseEachWord("Python Scripting is good learning");
		REWUStr.ParseEachWord(null);
		REWUStr.ParseEachWord("");
		/*
		 * create the class object ReversingEachWord_Using_LambdaExp, and call the
		 * method ParseAlphanumeric
		 */
		ReversingEachWord_Using_LambdaExp REWULamda = new ReversingEachWord_Using_LambdaExp();
		REWULamda.ParseEachWord("Javascript is double ended sword");
		REWULamda.ParseEachWord(null);
		REWULamda.ParseEachWord("");
		/*
		 * create the class object ReversingEachWord_Using_Stream, and call the method
		 * ParseAlphanumeric
		 */
		ReversingEachWord_Using_Stream REWUStream = new ReversingEachWord_Using_Stream();
		REWUStream.ParseEachWord("NodeJs is powerful");
		REWUStream.ParseEachWord(null);
		REWUStream.ParseEachWord("");
	}
}
OUTPUT:
Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Java developers are in demand'
This sentence 'Java developers are in demand' after reversing 'avaJ srepoleved era ni dnamed '


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing ''


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Python Scripting is good learning'
This sentence 'Python Scripting is good learning' after reversing 'nohtyP gnitpircS si doog gninrael '


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing ''


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing ' '


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Javascript is double ended sword'
This sentence 'Javascript is double ended sword' after reversing 'tpircsavaJ si elbuod dedne drows '


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing ''


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing ''


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'NodeJs is powerful'
This sentence 'NodeJs is powerful' after reversing '[sJedoN, si, lufrewop]'


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing '[]'


Feb 03, 2023 7:49:14 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.ReversingEachWordJ8'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:49:14 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing '[]'


The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/02/06/junit-test-for-reversing-each-word-of-sentence/