This is the first complete solution to reverse each word in the given input line of strings. This program uses the core logic to reverse each string. This code now only accepts alphanumeric strings instead of pure integers, symbols and null strings.
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner inpStr = new Scanner(System.in);
System.out.println("Enter an input sentence");
String inpLine = inpStr.nextLine();
if (inpLine.isEmpty() == true) {
System.out.println("Given string is empty");
} else {
String resultStr = "";
// Split the input sentence into list of words with space as delimiter
List<String> words = Arrays.asList(inpLine.split("\\s"));
// System.out.println(words);
if (isContainsValidWords(words) == false) {
System.out.println("Invalid input");
} else {
// Pass each word to the reverse function which returns the reversed string
// value
for (int i = 0; i < words.size(); i++) {
resultStr = resultStr + ReverseStringMethod(words.get(i)) + " ";
}
inpStr.close();
System.out.println("The output sentence after reversing each word is below");
System.out.println(resultStr);
}
}
}
private static boolean isContainsValidWords(List<String> words1) {
/*
* This method checks if every single word in the given string is valid. It goes
* through the list of words and uses the method "isValidWord" on each word.
* Using a counter variable (validWordCount), if the no. of valid words in the
* string is equal to the length of string, it returns true
*/
int validWordCount = 0;
for (int i = 0; i < words1.size(); i++) {
String tmpStr = words1.get(i);
if (isValidWord(tmpStr))
validWordCount++;
}
if (validWordCount == words1.size())
return true;
else
return false;
}
private static String ReverseStringMethod(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;
}
public static boolean isValidWord(String s) {
/*
* This method uses regular expressions to first check if the given word
* consists of only numbers and then, check if it has any symbols. If it doesn't
* only have numbers and doesn't have any symbols, it's a valid word.
*/
boolean onlyNumberChecker = s.matches("[0-9]*$") && s.matches("[^a-zA-Z]*$");
boolean noSymbolChecker = s != null && s.matches("^[a-zA-Z0-9]*$");
if (onlyNumberChecker == false && noSymbolChecker == true)
return true;
else if (onlyNumberChecker)
return false;
else
return false;
}
}
First set of inputs:
Enter an input sentence
Code does not accept integers
The output sentence after reversing each word is below
edoC seod ton tpecca sregetni
Second set of inputs:
Enter an input sentence
I weigh 57 kilograms
Invalid input
Third set of inputs:
Enter an input sentence
Hexadecimal numbers include 2359AF
The output sentence after reversing each word is below
lamicedaxeH srebmun edulcni FA9532
Fourth set of inputs:
Enter an input sentence
Given string is empty

Leave a comment