×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java Applet Basic Numbers Program In Java Part 2
Java Examples : Frequency of the character in a paragraph - Java
3707    Arnab De    31/08/2017

Q. Write a program to display the frequency of the character in a paragraph using java.

Here accept a string from the user by the method nextLine(). which is found under the Scanner class of java.util package. now extract each character from it. if the character is alphabetic then increment the corresponding counter. Now print those counters one after another. The details 


softetechnologies

import java.util.*;
class FrequencyChar
{
public static void main(String []args)
{
String str;
int []arr=new int[26];
Scanner sc=new Scanner(System.in);
System.out.print("Enter Your Text Here : ");
str=sc.nextLine();
str=str.toUpperCase();
char ch;
for(int i=0;i<str.length();i++)
{
ch=str.charAt(i); 
if(ch>=65 && ch<=90)
arr[ch-65]++;
}
for(int j=0;j<26;j++)
{
if(arr[j]>0)
System.out.println(((char)(j+65)) + " : " + arr[j]);
}
}
}

softetechnologies

Here we take an integer array of length 26. Indexes of this array represent the character A-Z. That is Index 0 is represent A, Index 1 represents B, Index 2 represents C …… and so on.

  • Convert our text into the upper case using the toUpperCase() method.
  • Extract each character from the string. If that Character is alphabet that is ASCII code within 65 -90. Then increase the value of the corresponding index value. Calculate the index as follows
    • If Character A then ASCII code is 65. The array index will be 65-65=0.
    • Now print only those array elements which values are not zero.
Java Applet Basic Numbers Program In Java Part 2
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
Education is not the learning of facts. It is rather the training of the mind to think
Albert Einstein
29148
52.78
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     42033
01/01/2018     36004
28/06/2017     34089
25/06/2018     33448
02/08/2017     32401
01/08/2017     26869
06/07/2017     26791
15/05/2017     26418
14/07/2017     21855
21/04/2018     20717
softetechnologies