master
  1/**
  2 * Assignment 2, COMP268 Class: ReversedSentence.java
  3 *
  4 * @description Provides a class to manipulating strings.
  5 * @author: mo khan Student ID: 3431709
  6 * @date Jun 2, 2019
  7 * @version 1.0
  8 */
  9package Q1;
 10
 11import java.util.Scanner;
 12
 13public class ReversedSentence {
 14  /**
 15   * Replaces the every character that is in an index that is evenly divisible by 5 with the
 16   * character 'z', excluding 0.
 17   *
 18   * @param s The input string to substitue
 19   * @return The new string with the substitution.
 20   */
 21  public static String change5thPosition(String s) {
 22    char[] result = new char[s.length()];
 23
 24    for (int i = 0; i < s.length(); i++) result[i] = (i > 0 && i % 5 == 0) ? 'z' : s.charAt(i);
 25
 26    return new String(result);
 27  }
 28
 29  /**
 30   * Combines the 2D array of character into a printable string.
 31   *
 32   * @param arr the two dimensional array of characters.
 33   * @return a String of the combined character arrays separated by a line separator.
 34   */
 35  public static String printChar2DArray(char[][] arr) {
 36    String[] strings = new String[arr.length];
 37
 38    for (int i = 0; i < arr.length; i++) strings[i] = new String(arr[i]);
 39    return String.join(System.lineSeparator(), strings);
 40  }
 41
 42  /**
 43   * Reverses a string.
 44   *
 45   * @param s the input String to reverse
 46   * @return the reversed String.
 47   */
 48  public static String reverseByCharacter(String s) {
 49    char[] result = new char[s.length()];
 50    int length = s.length();
 51
 52    for (int i = 0; i < length; i++) result[length - i - 1] = s.charAt(i);
 53
 54    return new String(result);
 55  }
 56
 57  /**
 58   * Reverses the words in a string
 59   *
 60   * @param s the input string to reverse
 61   * @return a string with the words in reverse order
 62   */
 63  public static String reverseByWord(String s) {
 64    String[] words = s.split(" ");
 65    String[] result = new String[words.length];
 66
 67    for (int i = 0; i < words.length; i++) {
 68      String word = words[i];
 69      result[words.length - i - 1] = word;
 70    }
 71    return String.join(" ", result);
 72  }
 73
 74  /**
 75   * Truncates a tring to a maximum of 80 characters.
 76   *
 77   * @param s the input string to truncate
 78   * @return the truncated string
 79   */
 80  public static String truncateSentence(String s) {
 81    return s.substring(0, Math.min(s.length(), 80));
 82  }
 83
 84  /**
 85   * The entry point for the console application.
 86   *
 87   * @param args the commandline arguments passed to the program
 88   */
 89  public static void main(String[] args) {
 90    Scanner in = new Scanner(System.in);
 91    String[] sentences = new String[3];
 92
 93    for (int i = 0; i < 3; i++) {
 94      System.out.println(String.format("Enter sentence %d: (max 80 characters)", i + 1));
 95      sentences[i] = ReversedSentence.truncateSentence(in.nextLine());
 96    }
 97
 98    char[][] matrix = new char[3][80];
 99    matrix[0] = ReversedSentence.reverseByCharacter(sentences[0]).toCharArray();
100    matrix[1] = ReversedSentence.reverseByWord(sentences[1]).toCharArray();
101    matrix[2] = ReversedSentence.change5thPosition(sentences[2]).toCharArray();
102
103    System.out.println();
104    System.out.println("Result: ");
105    System.out.println();
106    System.out.print(ReversedSentence.printChar2DArray(matrix));
107
108    System.out.println();
109    System.out.println();
110    System.out.println("Bye");
111  }
112}