-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharFreq.c
89 lines (75 loc) · 1.48 KB
/
charFreq.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define debug if(0)
typedef struct node{
char a;
int freq;
struct node* next;
}node;
node* init()
{
return NULL;
}
node* add(node* head, char item, int freq)
{
node* new_node = (node*) malloc(sizeof(node*));
new_node->a = item;
new_node->freq = freq;
new_node->next = head;
return new_node;
}
void printer(node* head)
{
while (head != NULL)
{
printf("%c %d\n", head->a, head->freq);
head = head->next;
}
}
node* search(node* head, char data)
{
while (head != NULL)
{
if (head->a == data)
{
return head;
}
head = head->next;
}
return NULL;
}
int main()
{
char str[1000]; //input
int i = 0;
fgets(str,1000,stdin);
int max = strlen(str); //tamanho da string input
char* pch;
int freq;
node* lista = init();
int ch[256] = {0}; //index == ascii, value == freq
for (int i = 0; i < 256 ; i++)
{
freq = 0;
char tmp = i; //str[i] = 'a'
for (int j = 0; j < max; j++)
{
if (tmp == str[j])
{
freq++;
}
}
ch[i] = freq;
debug printf("[%c] [%d]\n",(char)i,freq);
}
for (int aux = 0; aux < 256 ; aux++)
{
if (ch[aux] > 0)
{
lista = add(lista,(char)aux,ch[aux]);
}
}
printer(lista);
return 0;
}