×
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
3849    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
Education is not the amount of information that is put into your brain and runs riot there, undigest
Swami Vivekananda
1062
56.33
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43300
01/01/2018     36373
25/06/2018     35080
28/06/2017     34466
02/08/2017     32882
01/08/2017     27363
06/07/2017     27138
15/05/2017     26754
14/07/2017     22365
21/04/2018     21010
softetechnologies