- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
import java.util.ArrayList;
public class Chapter19 {
/* find repeat chars in text (in all words of text)
* print repeat chars
*
*/
private String stringArray[] = { "Allocates ae neaw Setringa tehat",
"represeants tahe same saequence " };
final private String alfabetArray = "abcdefghijklmnopqrstuvwxyz";
private ArrayList <Character> repeatChars;
public void run() {
printStringArray();
repeatChars = new ArrayList<Character>();
extractRepeatChars();
if (!repeatChars.isEmpty()) {
printRepeatChars();
} else {
System.out.println("not repeat ");
}
}
private void printRepeatChars(){
System.out.println("");
for (char c : repeatChars) {
System.out.println(c);
}
}
private void printStringArray(){
System.out.println(" ");
for (String s : stringArray) {
System.out.println(s);
}
}
public String [][] parseStringArray() {
String wordsArray[][] = new String[stringArray.length][];
for (int i = 0; i < wordsArray.length; i++) {
wordsArray[i] = stringArray[i].split("\n|\t| ");
}
return wordsArray;
}
public int findRepeatCharInWordsArray(String [][]wordsArray, char c) {
for (int i = 0; i < wordsArray.length; i++) {
for (int j = 0; j < wordsArray[i].length; j++) {
if (wordsArray[i][j].indexOf(c) < 0) {
return 0; // zodyje c nerastas
}
}
}
return 1;
}
public void extractRepeatChars() {
String wordsArray[][] = parseStringArray();
for (char c : alfabetArray.toCharArray()) {
if (findRepeatCharInWordsArray(wordsArray, c) > 0) {
repeatChars.add(c);
}
}
}
} // end
Follow us!