forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubloon.java
More file actions
63 lines (56 loc) · 1.73 KB
/
Doubloon.java
File metadata and controls
63 lines (56 loc) · 1.73 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
/**
* Created by media on 2016-07-17.
*/
public class Doubloon {
public static String readInput(String paramName){
Scanner in = new Scanner(System.in);
System.out.print("Write " + paramName + ": ");
return in.next();
}
/*
* Returns TRUE when input char is in array
*/
public static boolean isCharInArray(char letter, char[] array){
boolean okay = false;
for(int i=0; i<array.length; i++){
if(letter == array[i]){
okay = true;
break;
}
}
return okay;
}
/*
* This function returns input string without character with input index
*/
public static String removeLetterByIndex(String s, int index){
return s.substring(0,index) + s.substring(index+1,s.length());
}
public static boolean isDoubloon(String s){
int noOfRepetedLetters = 0;
String tmp = "";
char letter;
char[] searchedLetters = new char[s.length()];
boolean okay = true;
for(int i=0; i<s.length(); i++){
if(!isCharInArray(s.charAt(i), searchedLetters)){
letter = s.charAt(i);
tmp = removeLetterByIndex(s,i);
if(tmp.indexOf(letter) != -1 ){
searchedLetters[noOfRepetedLetters] = letter;
noOfRepetedLetters++;
} else{
okay = false;
break;
}
}
}
return okay;
}
public static void main(String[] args){
String userWord = readInput("your word");
userWord.toLowerCase();
System.out.print("Result: " + isDoubloon(userWord));
}
}