From 33f8aa39e1b7dfdaea4cf01b063491c7a72bdc87 Mon Sep 17 00:00:00 2001 From: mayfff <84086579+mayfff@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:15:35 +0200 Subject: [PATCH] Create ThirdLab.java --- .../lab3/ThirdLab.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/_12_Закревський_Данило_Сергійович/lab3/ThirdLab.java diff --git a/src/_12_Закревський_Данило_Сергійович/lab3/ThirdLab.java b/src/_12_Закревський_Данило_Сергійович/lab3/ThirdLab.java new file mode 100644 index 0000000..2c39381 --- /dev/null +++ b/src/_12_Закревський_Данило_Сергійович/lab3/ThirdLab.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +public class ThirdLab { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + System.out.print("Enter a string: "); + String text = scn.nextLine(); + StringBuilder textBuilder = new StringBuilder(text); + System.out.print("Enter a string to replace the words: "); + String replace = scn.nextLine(); + System.out.print("Enter the length of the words to replace: "); + int length = scn.nextInt(); + String[] words = textBuilder.toString().split("\\s+"); + textBuilder.setLength(0); + for(String word : words) { + if(word.charAt(word.length()-1) == '.' && word.length() - 1 == length) { + textBuilder.append(replace + ". "); + } else if (word.charAt(word.length() - 1) == '!' && word.length() - 1 == length) { + textBuilder.append(replace + "! "); + } else if (word.charAt(word.length() - 1) == '?' && word.length() - 1 == length) { + textBuilder.append(replace + "? "); + } else if (word.length() == length) { + textBuilder.append(replace + " "); + } else { + textBuilder.append(word + " "); + } + } + + System.out.printf("Result: %s", textBuilder); + scn.close(); + } +}