blob: d218c8c5547a3743166ea08f8e020a64e542b9d2 (
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
57
58
59
60
61
62
63
64
65
66
67
68
|
package Q1;
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 s.substring(0, Math.min(s.length(), 80));
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] sentences = new String[3];
for (int i = 0; i < 3; i++) {
System.out.println(String.format("Enter sentence %d: ", i + 1));
sentences[i] = ReversedSentence.truncateSentence(in.nextLine());
}
char[][] matrix = new char[3][80];
matrix[0] = ReversedSentence.reverseByCharacter(sentences[0]).toCharArray();
matrix[1] = ReversedSentence.reverseByWord(sentences[1]).toCharArray();
matrix[2] = ReversedSentence.change5thPosition(sentences[2]).toCharArray();
System.out.println();
System.out.println("Result: ");
System.out.println();
System.out.print(ReversedSentence.printChar2DArray(matrix));
System.out.println();
System.out.println();
System.out.println("Bye");
}
}
|