Update lab3
This commit is contained in:
parent
1f0c160216
commit
f07b3b6617
31
lab3
31
lab3
|
@ -1,6 +1,6 @@
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class lab3 {
|
public class Lab5 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
int C17 = 2430 % 17;
|
int C17 = 2430 % 17;
|
||||||
|
@ -19,16 +19,40 @@ public class lab3 {
|
||||||
String[] words = sb.toString().split("\\s+");
|
String[] words = sb.toString().split("\\s+");
|
||||||
for (int i = 0; i < words.length; i++) {
|
for (int i = 0; i < words.length; i++) {
|
||||||
String word = words[i];
|
String word = words[i];
|
||||||
char lastLetter = word.charAt(word.length() - 1);
|
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 = "";
|
String newWord = "";
|
||||||
for (int j = 0; j < word.length() - 1; j++) {
|
for (int j = 0; j < word.length() - 1; j++) {
|
||||||
if (word.charAt(j) != lastLetter) {
|
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 += word.charAt(j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newWord += lastLetter;
|
newWord += lastLetter;
|
||||||
words[i] = newWord;
|
words[i] = newWord;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
System.out.print("\nFinal string: ");
|
System.out.print("\nFinal string: ");
|
||||||
System.out.println(String.join(" ", words));
|
System.out.println(String.join(" ", words));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -36,3 +60,4 @@ public class lab3 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue