program to find the duplicate characters in a string
In this program, we need to find the duplicate characters in the string. Great responsibility To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.
string 1
"Great responsibility"
Program
class duplicate_char {
public static void main(String[] args) {
//declare string
String str1="Great responsibility";
int count;
char str1[]=str.toCharArray();
System.out.println( "Duplicate charcters in given string" );
for (int i=0;i< str1.length;i++)
{
count=1;
forint j=i+1; j< str1.length;j++)
{
if (str1[i]==str1[j] && str1[i]!=' ')
System.out.println(str1[j]);
break;
}
}
}
}
output
Duplicate characters in a given string: r e t s i
Theory
No comments:
Post a Comment