forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency_of_chars.cpp
152 lines (152 loc) · 2.92 KB
/
frequency_of_chars.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int ch,i,j,k,l,fc[50][2],flag;
char line[50],fw[50][50];
clrscr();
do
{
printf("\n<1> Input a line");
printf("\n<2> Number of characters");
printf("\n<3> Number of words");
printf("\n<4> Number of vowals");
printf("\n<5> Frequency of characters");
printf("\n<6> Frequency of words");
printf("\n<7> Exit\n");
do
{
printf("\n enter your choice ");
scanf("%d",&ch);
}while(ch>7 || ch<1);
switch (ch)
{
case 1: //input a line
printf("\n Enter a line ");
scanf("\n%[^\n]",line);
break;
case 2: //number of characters
j=0;
for(i=0;i<strlen(line);++i)
{
if(line[i]>64 && line[i]<93)
++j;
else
if(line[i]>96 && line[i]<123)
++j;
}
printf("total number of characters are %d",j);
break;
case 3: //number of words
j=0;
for(i=0;i<strlen(line);++i)
{
if(line[i]==' ' || line[i]=='\t')
continue;
else
{
for(k=i;k<strlen(line);++k)
{
if(line[k]==' ' || line[k]=='\t')
break;
else
++i;
}
}
++j;
}
printf("\n total number of words are %d",j);
break;
case 4: //number of vowels
j=0;
for(i=0;i<strlen(line);++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u')
++j;
else
if(line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++j;
}
printf("total number of vowels are %d",j);
break;
case 5: //frequency of characters
for(i=0;i<strlen(line);++i)
{
fc[i][1]=0;
flag=0;
for(j=0;j<strlen(line);++j)
{
if(line[i]==fc[j][0])
{
fc[j][0]=line[i];
++fc[j][1];
flag=1;
break;
}
}
if(flag==0)
fc[i][0]=line[i],fc[i][1]=1;
}
printf("\n Frequency of each characters");
for(i=0;i<strlen(line);++i)
if(fc[i][1]!=0)
printf("\n%c %d",fc[i][0],fc[i][1]);
break;
case 6: //frequency of words
j=0;
for(i=0;i<strlen(line);++i)
{
if(line[i]==' ' || line[i]=='\t')
continue;
else
{
l=0;
for(k=i;k<strlen(line);++k)
{
if(line[k]==' ' || line[k]=='\t')
break;
else
{
fw[j][l]=line[k],fw[j][++l]='\0';
++i;
}
}
}
++j;
}
for(i=0;i<j;++i)
{
fc[0][0]=0;
for(k=i;k<j;++k)
{
flag=1;
for(l=0;l<j;++l)
if(fw[i][l]!=fw[k][l])
flag=0;
if(flag==1)
{
++fc[0][0];
if(fc[0][0]>1)
fw[k][0]='\0';
}
}
if(fc[0][0]>0)
{
flag=0;
do
{
printf("%c",fw[i][flag]);
} while(fw[i][flag++]!='\0');
if(fw[i][0]!='\0')
printf("%d\n",fc[0][0]);
}
fc[0][0]=0;
}
break;
}
}while(ch!=7);
printf("\n\n i will wait for your mails ");
getch();
return ;
}