-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
37 lines (33 loc) · 1.37 KB
/
App.java
File metadata and controls
37 lines (33 loc) · 1.37 KB
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
import java.util.Scanner; //////работает!!
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder text = new StringBuilder(); // текст загоняем в стрингбилдер
int NumberOfLines = 0;
while (true) {
NumberOfLines++;
String inputText = scanner.nextLine();
if (inputText.isEmpty()) {
break;
}
text.append(inputText).append("\n");
}
System.out.print("Длина заменяемого слова: ");
int n = scanner.nextInt();
scanner.nextLine();
System.out.print("подстрока: ");
String replacementWord = scanner.nextLine();
String[] lines = text.toString().split("\n"); // разделяем текст на строки
StringBuilder outputText = new StringBuilder(); // результат в новом стрингбилдере
for (String line : lines) {
String[] words = line.split("\\s+");
for (String word : words) {
if (word.length() == n) {
line = line.replace(word, replacementWord);
}
}
outputText.append(line).append("\n");
}
System.out.println(outputText.toString().trim());
}
}