-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample4.java
66 lines (43 loc) · 1.33 KB
/
Example4.java
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
64
65
66
package applications.algorithms;
import java.util.Scanner;
/** Category: Algorithms
* ID: Example 4
* Description: Finds the first non-repeated character in a
* user specified string. It assumes ASCII characters
* Taken From:
* Details:
* TODO
*/
public class Example4 {
public static void find(String str){
if(str == null){
throw new NullPointerException("Input string instance in null");
}
if(str == ""){
System.out.println("Empty string was specified....");
return;
}
// we can have as much as 256 ASCII characters
int[] vals = new int[256];
for(int i=0; i<vals.length; ++i){
vals[i] = 0;
}
for(int i=0; i<str.length(); ++i){
int val = str.charAt(i);
vals[val] += 1;
}
for(int i=0; i<str.length(); ++i){
int val = str.charAt(i);
if(vals[val] == 1){
System.out.println("First non repeated character is: "+str.charAt(i));
break;
}
}
}
public static void main(String[] arg){
Scanner scanner = new Scanner(System. in);
String inputString = scanner.nextLine();
System.out.println("You entered: " + inputString);
find(inputString);
}
}