Java Code to Find Frequency of Characters in String - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Monday, September 25, 2017

Java Code to Find Frequency of Characters in String

Write a Java Code to Find Frequency of Characters in String

Program:
/* Java Program Example - Find Frequency of Character */

import java.util.Scanner;

public class JavaProgram
{
public static void main(String args[])
{
int ci, i, j, k, l=0;
String str, str1;
char c, ch;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a String : ");
str=scan.nextLine();

i=str.length();
for(c='A'; c<='z'; c++)
{
k=0;
for(j=0; j<i; j++)
{
ch = str.charAt(j);
if(ch == c)
{
k++;
}
}
if(k>0)
{
System.out.println("The character " + c + " has occurred for " + k + " times");
}
}
}

}

OUTPUT
Enter a String : Learn in ProgramsforU
The character L has occurred for 1 times
The character P has occurred for 1 times
The character U has occurred for 1 times
The character a has occurred for 2 times
The character e has occurred for 1 times
The character f has occurred for 1 times
The character g has occurred for 1 times
The character i has occurred for 1 times
The character m has occurred for 1 times
The character n has occurred for 2 times
The character o has occurred for 2 times
The character r has occurred for 4 times
The character s has occurred for 1 times

No comments:

Post a Comment