Skip to content

Commit 3207811

Browse files
committed
need help biggest_seq + charfreq
1 parent f0317c8 commit 3207811

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

charFreq.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#define debug if(0)
5+
6+
typedef struct node{
7+
char a;
8+
int freq;
9+
struct node* next;
10+
}node;
11+
12+
node* init()
13+
{
14+
return NULL;
15+
}
16+
17+
node* add(node* head, char item, int freq)
18+
{
19+
node* new_node = (node*) malloc(sizeof(node*));
20+
21+
new_node->a = item;
22+
new_node->freq = freq;
23+
new_node->next = head;
24+
25+
return new_node;
26+
}
27+
28+
void printer(node*head)
29+
{
30+
while (head != NULL)
31+
{
32+
printf("%c %d\n", head->a, head->freq);
33+
head = head->next;
34+
}
35+
}
36+
node* search(node* head, char data)
37+
{
38+
while (head != NULL)
39+
{
40+
if (head->a == data)
41+
{
42+
return head;
43+
}
44+
head = head->next;
45+
}
46+
return NULL;
47+
}
48+
int main()
49+
{
50+
char str[1000];
51+
int i = 0;
52+
fgets(str,1000,stdin);
53+
int max = strlen(str);
54+
char* pch;
55+
int freq;
56+
node* lista = init();
57+
for (int i = 0; i < max ; i++)
58+
{
59+
freq = 0;
60+
char tmp = str[i];
61+
for (int j = 0; j < max; j++)
62+
{
63+
if (tmp == str[j])
64+
{
65+
freq++;
66+
}
67+
}
68+
debug printf("[%c] [%d]\n",str[i],freq);
69+
if (search(lista,tmp) == NULL)
70+
{
71+
lista = add(lista,tmp,freq);
72+
}
73+
}
74+
printer(lista);
75+
return 0;
76+
}
77+
//TODO #2 criar uma maneira de ordenar a saida na ordem anti-alfabética

0 commit comments

Comments
 (0)