This Java code will Print the number of occurrence for all unique character in the given String
Input - Test@Tutor
Output -
Occurrence of char [t] - 4
Occurrence of char [e] - 1
Occurrence of char [s] - 1
Occurrence of char [@] - 1
Occurrence of char [u] - 1
Occurrence of char [o] - 1
Occurrence of char [r] - 1
CODE -
public class CountCharOccurance
{
public static void main(String[] args)
{
String value = "Test@Tutor";
int[] count = new int[256];
value = value.toLowerCase();
for (int i = 0; i< value.length(); i++)
{
count[(int)value.charAt(i)]++;
}
List<Character> printed = new ArrayList<Character>();
for (int i = 0; i< value.length(); i++)
{
if (printed.contains(value.charAt(i)))
continue;
printed.add(value.charAt(i));
System.out.println("Occurrence of char [" + value.charAt(i) + "] - " + count[(int)value.charAt(i)]);
}
}
}
Input - Test@Tutor
Output -
Occurrence of char [t] - 4
Occurrence of char [e] - 1
Occurrence of char [s] - 1
Occurrence of char [@] - 1
Occurrence of char [u] - 1
Occurrence of char [o] - 1
Occurrence of char [r] - 1
CODE -
public class CountCharOccurance
{
public static void main(String[] args)
{
String value = "Test@Tutor";
int[] count = new int[256];
value = value.toLowerCase();
for (int i = 0; i< value.length(); i++)
{
count[(int)value.charAt(i)]++;
}
List<Character> printed = new ArrayList<Character>();
for (int i = 0; i< value.length(); i++)
{
if (printed.contains(value.charAt(i)))
continue;
printed.add(value.charAt(i));
System.out.println("Occurrence of char [" + value.charAt(i) + "] - " + count[(int)value.charAt(i)]);
}
}
}
No comments:
Post a Comment