×
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
4516    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.
Swami Vivekananda
Truth can be stated in a thousand different ways, yet each one can be true.
Swami Vivekananda
3857
75.03
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     52291
25/06/2018     44059
01/01/2018     42982
28/06/2017     40718
02/08/2017     39573
01/08/2017     33742
06/07/2017     33559
15/05/2017     32837
14/07/2017     29127
11/09/2018     29120
softetechnologies