×
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
3999    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
Do not worry about your difficulties in Mathematics. I can assure you mine are still greater.
Albert Einstein
2093
60.43
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     44655
01/01/2018     36856
25/06/2018     36685
28/06/2017     34876
02/08/2017     33472
01/08/2017     27830
06/07/2017     27599
15/05/2017     27166
14/07/2017     22912
11/09/2018     21630
softetechnologies