Update lab3
This commit is contained in:
parent
1f0c160216
commit
f07b3b6617
41
lab3
41
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 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue