0% found this document useful (0 votes)
18 views2 pages

String Class in Java

Write a program to count Frequency of a character in a given String. (String class)

Uploaded by

Aman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

String Class in Java

Write a program to count Frequency of a character in a given String. (String class)

Uploaded by

Aman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXP7:a) Write a program to count Frequency of a character in a

given String. (String class)


Source code:
import java.util.*;

//write a program to count frequency of a character in a given string (string class)

public class Test{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter a String: ");

String str = sc.nextLine();

int[] freq = new int[str.length()];

char string[] = str.toCharArray(); // converting string to an array of characters

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

freq[i] = 1;

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

if(string[i]==string[j]){

freq[i]++;

string[j] = '0';

System.out.println("Characters and their Frequencies are: ");

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

if(string[i]!=' ' && string[i]!='0'){

System.out.println(string[i]+" - "+freq[i]);

}
OUTPUT:

You might also like