Tuesday, April 16, 2013

To find the first repeated character in a string

Example: 
java
The repeated character in java is : a



public class FirstRepeatedCharacter {

/**
* @param args
*/
public static void main(String[] args) {


String str ="welcome";

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

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

}

/*
Output:
The first repeated character in a welcome  is: e
*/

No comments:

Post a Comment