This java
program finds duplicate characters in a string. In this program, we assign a
string to a variable named "str" and this string is converted to
character array using toCharArray() method and assigned to a variable
"c". Then using nested for loop, we compare each character of char
array to the other characters in array and if they match, we print that
character.
Java Program to Find Duplicate Characters in a String
class FindDuplicateString{
public static void main(String[] args){
String str = "java";
char[] c = str.toCharArray();
System.out.print("Duplicate characters : ");
for(int i=0; i<str.length(); i++){
for(int j=i+1; j<str.length(); j++){
if(c[i] == c[j]){
System.out.print(c[i]+" "); }
}
}
}
}
Output:
Duplicate characters : a
0 comments:
Post a Comment