From f07b3b6617de864a6bd4c51fb90fea9bee2471e6 Mon Sep 17 00:00:00 2001 From: kxtzzl <123520267+romchhh@users.noreply.github.com> Date: Thu, 11 May 2023 00:50:04 +0300 Subject: [PATCH] Update lab3 --- lab3 | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lab3 b/lab3 index 378f3d8..ba23f99 100644 --- a/lab3 +++ b/lab3 @@ -1,6 +1,6 @@ import java.util.Scanner; -public class lab3 { +public class Lab5 { public static void main(String[] args) { int C17 = 2430 % 17; @@ -19,15 +19,39 @@ public class lab3 { String[] words = sb.toString().split("\\s+"); for (int i = 0; i < words.length; i++) { String word = words[i]; - char lastLetter = word.charAt(word.length() - 1); - String newWord = ""; - for (int j = 0; j < word.length() - 1; j++) { - if (word.charAt(j) != lastLetter) { - newWord += word.charAt(j); + char lastChar = word.charAt(word.length() - 1); + if (!Character.isLetterOrDigit(lastChar)) { + // Last character is a punctuation mark + int lastLetterIndex = -1; + for (int j = word.length() - 2; j >= 0; j--) { + if (Character.isLetter(word.charAt(j))) { + lastLetterIndex = j; + break; + } } + if (lastLetterIndex != -1) { + char lastLetter = word.charAt(lastLetterIndex); + String newWord = ""; + for (int j = 0; j < word.length() - 1; j++) { + if (!Character.isLetterOrDigit(word.charAt(j)) || word.charAt(j) != lastLetter) { + newWord += word.charAt(j); + } + } + newWord += lastChar; + words[i] = newWord; + } + } else { + // Last character is a letter or a digit + char lastLetter = lastChar; + String newWord = ""; + for (int j = 0; j < word.length() - 1; j++) { + if (!Character.isLetterOrDigit(word.charAt(j)) || word.charAt(j) != lastLetter) { + newWord += word.charAt(j); + } + } + newWord += lastLetter; + words[i] = newWord; } - newWord += lastLetter; - words[i] = newWord; } System.out.print("\nFinal string: "); System.out.println(String.join(" ", words)); @@ -36,3 +60,4 @@ public class lab3 { } } } +