Thursday, December 1, 2016

Find first non repeated character in a string


public class FirstNonRepeatedChar {

public static void main(String[] args) {
String input = "InputTextYOUWant";
char[] c = input.toCharArray();
int count = 0;
for (int i = 0; i < c.length; i++) {
for (int j = i + 1; j < c.length; j++) {
if (String.valueOf(c[i]).toLowerCase().equals(String.valueOf(c[j]).toLowerCase())) {
count++;
break;
}

}

if (count == 0) {

System.out.println("Non Repeated Charatcher is:"+c[i]);
//if you want all non repeated values just comment below break
break;
} else {
count = 0;
}

}

}
}

No comments:

Post a Comment