Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignments;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- //import org.apache.commons.lang3.StringUtils;
- public class cs491f14_01_reverse_words {
- public static void main(String[] args) {
- // our object that will reverse our string
- cs491f14_01_reverse_words rw = new cs491f14_01_reverse_words();
- // input string
- String inputString = rw.receiveInput();
- // object with punctuation information from our input string
- PuncData puncData = rw.getPunc(inputString);
- // gives us our string reversed and without punctuation
- StringBuilder noPuncRevString = rw.removePuncRevString(inputString);
- // adds punctuation back
- String finalString = rw.addPuncToRevString(noPuncRevString, puncData);
- System.out.println(finalString);
- }
- /**
- * handles input of
- */
- public String receiveInput() {
- // System.out.println("Input: ");
- Scanner input = new Scanner(System.in);
- String r = input.nextLine();
- input.close();
- return r;
- }
- // find all punctuation, grab it
- public PuncData getPunc(String inputString) {
- HashMap<Integer, Character> puncs = new HashMap<>();
- ArrayList<Integer> indexes = new ArrayList<Integer>();
- Pattern testPattern = Pattern.compile("[,.?:;]");
- Pattern whiteSpace = Pattern.compile(" ");
- String testChar;
- int currentWordCount = 0;
- for (int i = 0; i < inputString.length(); i++) {
- testChar = "" + inputString.charAt(i);
- Matcher m = testPattern.matcher(testChar);
- Matcher w = whiteSpace.matcher(testChar);
- if (m.find()) {
- puncs.put(currentWordCount, inputString.charAt(i));
- indexes.add(currentWordCount);
- } else if (w.find()) {
- currentWordCount++;
- }
- }
- return new PuncData(puncs, indexes);
- }
- public StringBuilder removePuncRevString(String inputString) {
- StringBuilder sBuilder = new StringBuilder();
- Pattern testPattern = Pattern.compile("[^,.?:;]");
- String testChar;
- // remove punctuation
- for (int i = 0; i < inputString.length(); i++) {
- testChar = "" + inputString.charAt(i);
- Matcher m = testPattern.matcher(testChar);
- if (m.find()) {
- sBuilder.append(inputString.charAt(i));
- }
- }
- // reverse character array
- String newString = sBuilder.toString();
- String[] splitNewString = newString.split(" ");
- StringBuilder revString = new StringBuilder();
- StringBuilder endString = new StringBuilder();
- for (int i = splitNewString.length; i >= 0; i--) {
- endString.append(splitNewString[i].lastIndexOf(i));
- endString.append(".");
- }
- for (int i = splitNewString.length - 1; i >= 0; i--) {
- revString.append(splitNewString[i]);
- revString.append(" ");
- }
- return revString;
- // return endString;
- }
- public String addPuncToRevString(StringBuilder noPuncRevString,
- PuncData puncData) {
- // combined reversed string and punctuation
- StringBuilder finalString = noPuncRevString;
- Pattern testPattern = Pattern.compile(" ");
- String testChar;
- int currentWord = 0;
- boolean isEmpty = false;
- boolean isCurrent = false;
- boolean lastWasWhite = false;
- boolean justInserted = false;
- int j = 0;
- for (int i = 0; i < noPuncRevString.length(); i++) {
- testChar = "" + noPuncRevString.charAt(i);
- Matcher m = testPattern.matcher(testChar);
- if (m.find() && !lastWasWhite && !justInserted) {
- lastWasWhite = true;
- // check if we need a punctuation here
- isEmpty = puncData.getIndexes().isEmpty();
- isCurrent = (puncData.getIndexes().get(j) == currentWord);
- if (!isEmpty && isCurrent) {
- j++;
- if (!((puncData.getPunc().get(new Integer(currentWord))) == null)) {// make
- // sure
- // were
- // not
- // doing
- // it
- // over
- // again
- char insertPunc = puncData.getPunc().get(
- new Integer(currentWord));
- finalString.insert(i, insertPunc);
- justInserted = true;
- i++;
- // finished with that punctuation, delete it from the
- // hash
- puncData.getPunc().remove(insertPunc);
- }
- }
- currentWord++;
- } else {
- justInserted = false;
- lastWasWhite = false;
- }
- }
- return finalString.toString();
- }
- class PuncData {
- private HashMap<Integer, Character> punc;
- private ArrayList<Integer> indexes;
- public PuncData(HashMap<Integer, Character> punc,
- ArrayList<Integer> indexes) {
- this.punc = punc;
- this.indexes = indexes;
- }
- public ArrayList<Integer> getIndexes() {
- return indexes;
- }
- public HashMap<Integer, Character> getPunc() {
- return punc;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement