Monday, April 15, 2013

Find the first non repeated character in a string



To find the first non repeated character in a string
Example:  madam
The non repeated character in madam is : d



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NonRepeatCharacter {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br  = new BufferedReader(input);

System.out.println("Enter String value :");
String str = br.readLine();

for(int i=0;i<str.length();i++){
int count = 0;
for(int j=0;j<str.length();j++){

if(i!=j && str.charAt(i) == str.charAt(j)){
count = count +1 ;
break;
}
}
if(count == 0){
System.out.println("The first non repeated character is : " +str.charAt(i));
break;
}
}

}

}

/*
Output:

Enter String value :
character
The first non repeated character is : h

*/

No comments:

Post a Comment