Question: How to find the first repeated character in a String?
The below java program has been written in easy way to make the beginners understand how easy the way of coding is and how easily a concept can be achieved through programming.
The below java program has been written in easy way to make the beginners understand how easy the way of coding is and how easily a concept can be achieved through programming.
import java.util.Scanner;
public class StrRepeat {
public static void main(String args[]){
//Get the string to check the non repreated character
System.out.println("Enter the String: ");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char fin = checkLetter(str);
System.out.println("The character is: " + fin);
s.close();
}
//To check each individual char for non repeated
private static char checkLetter(String str) {
// TODO Auto-generated method stub
//To split the string to character
char[] ary = str.toCharArray();
char val = 0;
//To get the length of character
int len = ary.length;
char temp[] = new char[len];
for (int i=0; i<len; i++){
for (int j=0; j<len; j++){
if (j!= i){
if(ary[i] == ary[j]){
temp[i] = '$';
}
}
}
}
for (int k = 0; k< len; k++){
if (temp[k] == '$' ){
val = ary[k];
break;
}
}
return val;
}
}
Output:
Enter the String: lmadam
The character is: m