-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path10_find_frequency.c
62 lines (50 loc) · 1.48 KB
/
10_find_frequency.c
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
// // Write a program in C to Find the Frequency of Characters.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define ARRAY_SIZE 31
// // Main Function Start
int main()
{
char str[ARRAY_SIZE], copyStr[ARRAY_SIZE], length = 0, count;
printf("\nEnter Any String (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
fgets(str, ARRAY_SIZE, stdin); // // Input String
str[strcspn(str, "\n")] = '\0'; // // Replace '\n' character with '\0' in str
// // Find Length
while (str[length])
length++;
// // Copy str into copyStr
int i = 0;
for (i = 0; str[i]; i++)
copyStr[i] = str[i];
copyStr[i] = '\0'; // // Add Null Character at End
// // Sort copyStr
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - 1 - i; j++)
{
if (copyStr[j] > copyStr[j + 1])
{
char temp = copyStr[j];
copyStr[j] = copyStr[j + 1];
copyStr[j + 1] = temp;
}
}
}
// // Find and Print Frequency of each character
for (int i = 0; i < length; i += count)
{
count = 1;
for (int j = i + 1; j < length && copyStr[i] == copyStr[j]; j++)
count++;
if (copyStr[i] == ' ')
printf("\nFrequecy of Spaces => %d", count);
else
printf("\nFrequecy of %c => %d", copyStr[i], count);
}
putch('\n');
getch();
return 0;
}
// // Main Function End