This program in each word of the text removes the same letters as the first.
Example:
[In]: Hehllo, what's up - Hah.
[Out]: Hello, what's up - Ha
But I ran into a problem in punctuation. I donβt know how to associate a ΠΡΠ½ΠΊΡΡΠ°ΡΠΈΡ class with a Π‘Π»ΠΎΠ²ΠΎ / ΠΡΠ΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ class, so that, let's say, the '-' sign is not a Π‘Π»ΠΎΠ²ΠΎΠΌ , since my word is separated by spaces on both sides. And that after each sentence put point \ question. mark - object ΠΡΠ½ΠΊΡΡΠ°ΡΠΈΡ .
Code:
public class Lab4 { public static void main(String[] args){ String s = "Π Π°Π·ΠΌΠ΅Ρ ΠΈΠ»ΠΈ Π΄Π»ΠΈΠ½Π° ΠΌΠ°ΡΡΠΈΠ²Π° β ΡΡΠΎ ΠΎΠ±ΡΠ΅Π΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π² ΠΌΠ°ΡΡΠΈΠ²Π΅." + " Π Π°Π·ΠΌΠ΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π° Π·Π°Π΄Π°ΡΡΡΡ ΠΏΡΠΈ ΡΠΎΠ·Π΄Π°Π½ΠΈΠΈ ΠΌΠ°ΡΡΠΈΠ²Π° ΠΈ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΠ·ΠΌΠ΅Π½ΡΠ½ Π² Π΄Π°Π»ΡΠ½Π΅ΠΉΡΠ΅ΠΌ," + " Π΄ΡΡΠ³ΠΈΠΌΠΈ ΡΠ»ΠΎΠ²Π°ΠΌΠΈ Π½Π΅Π»ΡΠ·Ρ ΡΠ±ΡΠ°ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ ΠΈΠ· ΠΌΠ°ΡΡΠΈΠ²Π° ΠΈΠ»ΠΈ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΈΡ
ΡΡΠ΄Π°, Π½ΠΎ" + " ΠΌΠΎΠΆΠ½ΠΎ Π² ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ ΠΏΡΠΈΡΠ²ΠΎΠΈΡΡ Π½ΠΎΠ²ΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ."; Text text = new Text(s); System.out.println("Initial text:"); System.out.println(text.getAllText()); StringBuilder newText = new StringBuilder(); Sentence c; Word w; for(int i = 0; i < text.getSentences().length; i++){ c = new Sentence(text.getSentenceByIndex(i)); for(int j = 0; j < c.getWords().length; j++){ w = new Word(c.getWords()[j].getAllWord()); newText.append(w.removeChars()); newText.append(" "); } } System.out.println(newText); } } class Text { private Sentence[] sentences; private String allText; public Text(String allText){ String newAllText = allText.replaceAll("\\s+", " "); setAllText(newAllText); sentences = new Sentence[amountOfSentences()]; fillUpSentenceArray(); } public int amountOfSentences(){ return allText.split("\\.!?").length; } public void fillUpSentenceArray(){ for (int i = 0; i < amountOfSentences(); i++){ sentences[i] = new Sentence((allText.split("\\.!?"))[i]); } } public String getAllText() { return allText; } public Sentence[] getSentences() { return sentences; } public String getSentenceByIndex(int i){ return sentences[i].getAllSentence(); } public void setAllText(String allText) { this.allText = allText; } } class Sentence { private Word[] words; private Punctuation[] punctuations; private String allSentence; public Sentence(String allSentence){ this.allSentence = allSentence; words = new Word[countAmountOfWords()]; fillUpWordsArray(); punctuations = new Punctuation[countAmountOfPunct()]; fillUpPunctArray(); } public int countAmountOfWords(){ return allSentence.split(" ").length; } public int countAmountOfPunct(){ StringBuilder a = new StringBuilder(); for(int i = 0; i < allSentence.length(); i++){ if(allSentence.toCharArray()[i] == ',' || allSentence.toCharArray()[i] == '.' || allSentence.toCharArray()[i] == '-'){ a.append(allSentence.toCharArray()[i]); } } return a.length(); } public void fillUpWordsArray(){ for(int i = 0; i < countAmountOfWords(); i++){ words[i] = new Word((allSentence.split(" "))[i]); } } public void fillUpPunctArray(){ char[] charr = allSentence.toCharArray(); for(int i = 0; i < countAmountOfPunct(); i++){ if(charr[i] == ',' || charr[i] == '.' || charr[i] == '-'){ punctuations[i] = new Punctuation(charr[i]); } } } public Word[] getWords() { return words; } public String getAllSentence() { return allSentence; } } class Word { private Letter[] letters; private String allWord; public Word(String allWord){ this.allWord = allWord; letters = new Letter[countLengthofWord()]; fillUpLetterArray(); } public int countLengthofWord(){ return allWord.length(); } public void fillUpLetterArray(){ for (int i = 0; i < countLengthofWord(); i++){ letters[i] = new Letter(allWord.toCharArray()[i]); } } public StringBuilder removeChars(){ StringBuilder upDatedWord = new StringBuilder(countLengthofWord()); if (allWord.isEmpty()){ allWord = " "; } char distChar = allWord.charAt(0); upDatedWord.append(distChar); for (int i = 0; i < allWord.length(); i++){ if (allWord.isEmpty()){ continue; } if (Character.toLowerCase(distChar) != Character.toLowerCase(allWord.charAt(i))){ upDatedWord.append(allWord.charAt(i)); } } this.allWord = upDatedWord.toString(); return upDatedWord; } public String getAllWord() { return allWord; } } class Punctuation { private char symbol; public Punctuation(char symbol){ this.symbol = symbol; } public char getSymbol() { return symbol; } public void setSymbol(char symbol) { this.symbol = symbol; } } class Letter { private char letter; public Letter(char letter){ this.letter = letter; } public char getLetter() { return letter; } public void setLetter(char letter) { this.letter = letter; } } /*public void fillUpSentencePartArray(){ boolean flag = false; for(int i = 0; i < allSentence.toCharArray().length; i++){ if(Character.isLetter(allSentence.toCharArray()[i])){ char[] temp = new char[20]; temp[i] = allSentence.toCharArray()[i]; if(flag){ Word wordToAdd = new Word(temp.toString()); sentenceParts[i] = wordToAdd; } } else if(allSentence.toCharArray()[i] == '.' || allSentence.toCharArray()[i] == ','){ Punctuation znakToAdd = new Punctuation(allSentence.toCharArray()[i]); sentenceParts[i] = znakToAdd; } else flag = true; } } */