-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicates.java
More file actions
40 lines (33 loc) · 1.08 KB
/
duplicates.java
File metadata and controls
40 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
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
import java.util.Scanner;
//code to count the number ofr letters that are repeated in a word
public class duplicates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("please enter a word: ");
String word = sc.nextLine().toLowerCase();
int length = word.length();
String[] letters = new String[length];
int times = 0;
int index = 0;
for (int i = 0; i <length; i ++){
char c = word.charAt(i);
String s = String.valueOf(c);
boolean found = false;
for (int j = 0; j < index; j++){
if (letters[j].equals(s)){
found =true;
break;
}
}
if (found){
times ++;
}
else{
letters[index] =s;
index++;
}
}
System.out.println("Duplicates:" + times);
sc.close();
}
}