0% found this document useful (0 votes)
49 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)
49 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

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

given String. (String class)


Source code:
import [Link].*;

//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([Link]);

[Link]("Enter a String: ");

String str = [Link]();

int[] freq = new int[[Link]()];

char string[] = [Link](); // converting string to an array of characters

for(int i = 0; i<[Link](); i++){

freq[i] = 1;

for(int j = i + 1; j<[Link](); j++){

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

freq[i]++;

string[j] = '0';

[Link]("Characters and their Frequencies are: ");

for(int i = 0; i<[Link](); i++){

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

[Link](string[i]+" - "+freq[i]);

}
OUTPUT:

You might also like