Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. package assignments;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. //import org.apache.commons.lang3.StringUtils;
  10.  
  11. public class cs491f14_01_reverse_words {
  12.  
  13.     public static void main(String[] args) {
  14.         // our object that will reverse our string
  15.         cs491f14_01_reverse_words rw = new cs491f14_01_reverse_words();
  16.         // input string
  17.         String inputString = rw.receiveInput();
  18.         // object with punctuation information from our input string
  19.         PuncData puncData = rw.getPunc(inputString);
  20.         // gives us our string reversed and without punctuation
  21.         StringBuilder noPuncRevString = rw.removePuncRevString(inputString);
  22.         // adds punctuation back
  23.         String finalString = rw.addPuncToRevString(noPuncRevString, puncData);
  24.  
  25.         System.out.println(finalString);
  26.     }
  27.  
  28.     /**
  29.      * handles input of
  30.      */
  31.     public String receiveInput() {
  32.         // System.out.println("Input: ");
  33.  
  34.         Scanner input = new Scanner(System.in);
  35.         String r = input.nextLine();
  36.         input.close();
  37.         return r;
  38.     }
  39.  
  40.     // find all punctuation, grab it
  41.     public PuncData getPunc(String inputString) {
  42.  
  43.         HashMap<Integer, Character> puncs = new HashMap<>();
  44.         ArrayList<Integer> indexes = new ArrayList<Integer>();
  45.         Pattern testPattern = Pattern.compile("[,.?:;]");
  46.         Pattern whiteSpace = Pattern.compile(" ");
  47.  
  48.         String testChar;
  49.         int currentWordCount = 0;
  50.  
  51.         for (int i = 0; i < inputString.length(); i++) {
  52.             testChar = "" + inputString.charAt(i);
  53.  
  54.             Matcher m = testPattern.matcher(testChar);
  55.             Matcher w = whiteSpace.matcher(testChar);
  56.  
  57.             if (m.find()) {
  58.                 puncs.put(currentWordCount, inputString.charAt(i));
  59.                 indexes.add(currentWordCount);
  60.             } else if (w.find()) {
  61.                 currentWordCount++;
  62.             }
  63.         }
  64.         return new PuncData(puncs, indexes);
  65.     }
  66.  
  67.     public StringBuilder removePuncRevString(String inputString) {
  68.         StringBuilder sBuilder = new StringBuilder();
  69.  
  70.         Pattern testPattern = Pattern.compile("[^,.?:;]");
  71.         String testChar;
  72.  
  73.         // remove punctuation
  74.         for (int i = 0; i < inputString.length(); i++) {
  75.  
  76.             testChar = "" + inputString.charAt(i);
  77.             Matcher m = testPattern.matcher(testChar);
  78.  
  79.             if (m.find()) {
  80.                 sBuilder.append(inputString.charAt(i));
  81.             }
  82.         }
  83.  
  84.         // reverse character array
  85.         String newString = sBuilder.toString();
  86.         String[] splitNewString = newString.split(" ");
  87.         StringBuilder revString = new StringBuilder();
  88.         StringBuilder endString = new StringBuilder();
  89.         for (int i = splitNewString.length; i >= 0; i--) {
  90.             endString.append(splitNewString[i].lastIndexOf(i));
  91.             endString.append(".");
  92.         }
  93.         for (int i = splitNewString.length - 1; i >= 0; i--) {
  94.             revString.append(splitNewString[i]);
  95.             revString.append(" ");
  96.         }
  97.         return revString;
  98.         // return endString;
  99.     }
  100.  
  101.     public String addPuncToRevString(StringBuilder noPuncRevString,
  102.             PuncData puncData) {
  103.         // combined reversed string and punctuation
  104.         StringBuilder finalString = noPuncRevString;
  105.         Pattern testPattern = Pattern.compile(" ");
  106.         String testChar;
  107.  
  108.         int currentWord = 0;
  109.         boolean isEmpty = false;
  110.         boolean isCurrent = false;
  111.         boolean lastWasWhite = false;
  112.         boolean justInserted = false;
  113.         int j = 0;
  114.         for (int i = 0; i < noPuncRevString.length(); i++) {
  115.  
  116.             testChar = "" + noPuncRevString.charAt(i);
  117.             Matcher m = testPattern.matcher(testChar);
  118.  
  119.             if (m.find() && !lastWasWhite && !justInserted) {
  120.                 lastWasWhite = true;
  121.                 // check if we need a punctuation here
  122.                 isEmpty = puncData.getIndexes().isEmpty();
  123.                 isCurrent = (puncData.getIndexes().get(j) == currentWord);
  124.                 if (!isEmpty && isCurrent) {
  125.                     j++;
  126.                     if (!((puncData.getPunc().get(new Integer(currentWord))) == null)) {// make
  127.                                                                                         // sure
  128.                                                                                         // were
  129.                                                                                         // not
  130.                                                                                         // doing
  131.                                                                                         // it
  132.                                                                                         // over
  133.                                                                                         // again
  134.  
  135.                         char insertPunc = puncData.getPunc().get(
  136.                                 new Integer(currentWord));
  137.                         finalString.insert(i, insertPunc);
  138.                         justInserted = true;
  139.                         i++;
  140.                         // finished with that punctuation, delete it from the
  141.                         // hash
  142.                         puncData.getPunc().remove(insertPunc);
  143.                     }
  144.                 }
  145.                 currentWord++;
  146.             } else {
  147.                 justInserted = false;
  148.                 lastWasWhite = false;
  149.             }
  150.         }
  151.  
  152.         return finalString.toString();
  153.     }
  154.  
  155.     class PuncData {
  156.  
  157.         private HashMap<Integer, Character> punc;
  158.         private ArrayList<Integer> indexes;
  159.  
  160.         public PuncData(HashMap<Integer, Character> punc,
  161.                 ArrayList<Integer> indexes) {
  162.             this.punc = punc;
  163.             this.indexes = indexes;
  164.         }
  165.  
  166.         public ArrayList<Integer> getIndexes() {
  167.             return indexes;
  168.         }
  169.  
  170.         public HashMap<Integer, Character> getPunc() {
  171.             return punc;
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement