summaryrefslogtreecommitdiff
path: root/src/Q1/ReversedSentence.java
blob: 96a9793942820aa32d3f34c0b6d3aa641089d5ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package Q1;

import java.util.ArrayList;
import java.util.Scanner;

public class ReversedSentence {
  public static String change5thPosition(String s) {
    char[] result = new char[s.length()];

    for (int i = 0; i < s.length(); i++) result[i] = (i > 0 && i % 5 == 0) ? 'z' : s.charAt(i);

    return new String(result);
  }

  public static String printChar2DArray(char[][] arr) {
    String[] strings = new String[arr.length];

    for (int i = 0; i < arr.length; i++) strings[i] = new String(arr[i]);
    return String.join(System.lineSeparator(), strings);
  }

  public static String reverseByCharacter(String s) {
    char[] result = new char[s.length()];
    int length = s.length();

    for (int i = 0; i < length; i++) result[length - i - 1] = s.charAt(i);

    return new String(result);
  }

  public static String reverseByWord(String s) {
    String[] words = s.split(" ");
    String[] result = new String[words.length];

    for (int i = 0; i < words.length; i++) {
      String word = words[i];
      result[words.length - i - 1] = word;
    }
    return String.join(" ", result);
  }

  public static String truncateSentence(String s) {
    return "";
  }

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    ArrayList<String> sentences = new ArrayList<String>();

    for (int i = 0; i < 3; i++) {
      System.out.print("Enter sentence: ");
      String sentence = in.next();
      sentences.add(sentence);
    }
  }
}