-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHisto.java
More file actions
85 lines (82 loc) · 2.44 KB
/
Copy pathHisto.java
File metadata and controls
85 lines (82 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//WAP to print Horizontal and Vertical Histogram with Frequency Table.
import java.util.*;
public class Histo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a[]=new int[26];
String s;
System.out.println("Enter the text");
s=sc.nextLine();
generate_freq(s,a);
printTable(a);
print_horiz_histo(a);
print_vert_histo(a);
}
static char toLower(char ch) //Function to convert character to lower case.
{
if(ch>=65&&ch<=90)
ch=(char) (ch+32);
return ch;
}
static boolean isAlpha(char ch) //Function to check if character is alphabet.
{
if(ch>=97&&ch<=122)
return true;
else if(ch>=65&&ch<=90)
return true;
else
return false;
}
static void generate_freq(String s,int ctr[]) //Generating Frequency from Text file
{
char ch,ch1;
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(isAlpha(ch))
{
ch1=toLower(ch);
ctr[(int)ch1-97]++; //Counting frequency of letters
}
}
}
static void printTable(int a[]) //Function to print Frequency Table
{
System.out.println("Frequency Table \n\nLetter \t"+(char)166+" Frequency");
for(int i=1;i<=26;i++)
System.out.println((char)(i+96)+"\t"+(char)166+a[i-1]);
}
static void print_horiz_histo(int ctr[]) //Function to print Horizontal Histogram
{
System.out.println("\n Horizontal Histogram \n");
for(int i=1;i<=26;i++)
{
System.out.print((char)(i+96));
System.out.print((char)166);
for(int j=1;j<=ctr[i-1];j++)
System.out.print("*");
System.out.println();
}
}
static void print_vert_histo(int ctr[]) //Function to print Vertical Histogram
{
System.out.println("\n Vertical Histogram \n");
for(int i=25;i>=0;i--)
{
for(int j=0;j<26;j++)
{
if(ctr[j]-i>0)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
for(int k=0;k<26*2;k++)
System.out.print((char)45);
System.out.println();
for(int l=0;l<26;l++)
System.out.print((char)(l+97)+" ");
}
}