Dark Mode On/Off

# Java Program to find the frequency of character in string

In this tutorial, we will learn how to count the frequency of characters in a string. This means we will count which character is present how many times in the string.

This can be done by iterating through the string first and then calculating the number of times the characters have occurred. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the String: Study Tonight

Output: The characters and their corresponding frequencies:

Characters = Frequencies

S = 1

t = 2

u = 1

d = 1

y = 1

T = 1

o = 1

n = 1

i = 1

g = 1

h = 1

## Program 1: Count Frequency of Characters in a String

In this program, we will see how to count the frequency of a character in a string when the string is pre-defined in the program.

### Algorithm

1. Start

2. Declare a string

3. Initialize it.

4. Use a frequency array to store the frequency of each character.

5. Convert the string to a character array

6. Use two for loops to calculate the frequency of each element.

7. Use the first for loop to iterate through each character of the array.

8. Initialize each element of the frequency array as 1.

9. Use another for loop to iterate through the remaining characters.

10. Check for the total occurrence of the element.

11. If the element occurs again, increment the value in the frequency array.

12. Set the character array to 0 to avoid counting visited characters.

13. Print the characters and their corresponding frequency.

14. Stop.

Below is the Java code to count the frequency of characters in Java String.

``````/*Java Program to find the frequency of characters in a string*/
public class Main
{
public static void main(String[] args)
{
String str = "Study Tonight";
int[] freq = new int[str.length()];
System.out.println("The entered string is "+str);
//Convert the given string into character array
char str1[] = str.toCharArray();
for(int i = 0; i <str.length(); i++)
{
freq[i] = 1;
for(int j = i+1; j <str.length(); j++)
{
if(str1[i] == str1[j])
{
freq[i]++;

//Set str1[j] to 0 to avoid printing visited character
str1[j] = '0';
}
}
}
//Displays the characters and their corresponding frequency
System.out.println("Frequencies of the characters in the string are as below: ");
System.out.println("Characters  frequencies");
for(int i = 0; i <freq.length; i++)
{
if(str1[i] != ' ' && str1[i] != '0')
System.out.println(str1[i] + "              " + freq[i]);
}
}
}  ``````

The entered string is Study Tonight
Frequencies of the characters in the string are as below:
Characters frequencies
S 1
t 2
u 1
d 1
y 1
T 1
o 1
n 1
i 1
g 1
h 1

## Program 2: Count Frequency of Characters in a String

In this program, we will see how to count the frequency of a character in a string when the string is user-defined. Here, in this program, we will ask the user to enter a string and then we will calculate the frequency of the characters in the string.

### Algorithm

1. Start

2. Declare a string

3. Ask the user to initialize it.

4. Use a frequency array to store the frequency of each character.

5. Convert the string to a character array

6. Use two for loops to calculate the frequency of each element.

7. Use the first for loop to iterate through each character of the array.

8. Initialize each element of the frequency array as 1.

9. Use another for loop to iterate through the remaining characters.

10. Check for the total occurrence of the element.

11. If the element occurs again, increment the value in the frequency array.

12. Set the character array to 0 to avoid counting visited characters.

13. Print the characters and their corresponding frequency.

14. Stop.

Below is the Java code to count the frequency of characters in Java String.

``````/*Java Program to find the frequency of characters in a string*/
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
System.out.println("Enter the  string is: ");
String str = sc.nextLine();
int[] freq = new int[str.length()];   //Frequency array to store the frequency of each character
//Convert the given string into character array
char str1[] = str.toCharArray();
for(int i = 0; i <str.length(); i++)
{
freq[i] = 1;
for(int j = i+1; j <str.length(); j++)
{
if(str1[i] == str1[j])
{
freq[i]++;

//Set str1[j] to 0 to avoid printing visited character
str1[j] = '0';
}
}
}
//Displays the characters and their corresponding frequency
System.out.println("Frequencies of the characters in the string are as below: ");
System.out.println("Characters  frequencies");
for(int i = 0; i <freq.length; i++)
{
if(str1[i] != ' ' && str1[i] != '0')
System.out.println(str1[i] + "              " + freq[i]);
}
}
}  ``````

Enter the string is: Characters Frequency Count
Frequencies of the characters in the string are as below:
Characters frequencies
C 2
h 1
a 2
r 3
c 2
t 2
e 3
s 1
F 1
q 1
u 2
n 2
y 1
o 1