Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 7234104

Browse files
author
Ahmed Tawila
committed
first non-repeated character in a String
1 parent f1c47f8 commit 7234104

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package javarevisited.top.fifty.java.programs.from.coding.interviews;
2+
3+
/*Write a Java program to find the first non-repeated character in a String*/
4+
public class FirstNonRepeatedCharacterInAString {
5+
6+
public static char firstNonRepeatedCharacterInAString(String s) {
7+
char[] characters = s.toCharArray();
8+
int j;
9+
for (int i = 0; i < characters.length; i++) {
10+
for (j = 0; j < characters.length; j++) {
11+
if (i != j && characters[i] == characters[j])
12+
break;
13+
}
14+
if (j == characters.length)
15+
return characters[i];
16+
}
17+
return '0';
18+
}
19+
20+
public static void main(String[] args) {
21+
System.out.println(firstNonRepeatedCharacterInAString("Let's try this String!"));
22+
}
23+
}

0 commit comments

Comments
 (0)