-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercicio_39.cpp
63 lines (48 loc) · 1.36 KB
/
exercicio_39.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct palavra{
char * ptr;
unsigned int qtd;
}palavra;
int main(void){
char frase[100];
int qtdPalavras = 0;
//Ponteiro para trabalhar com arquivos
FILE * arquivo;
arquivo = fopen("teste1.txt", "r");
if(arquivo == NULL){
printf("Arquivo nao aberto");
return 0;
}
while(fgets(frase, 100, arquivo))
{
qtdPalavras++;
}
palavra palavras[qtdPalavras];
rewind(arquivo);
for(int i = 0; i < qtdPalavras; i++){
fgets(frase, 100, arquivo);
frase[strlen(frase)-1] = 0;
palavras[i].ptr = (char * ) malloc(sizeof(char) * (strlen(frase) + 1));
strcpy(palavras[i].ptr, frase);
palavras[i].qtd = 1;
}
for(int i = 0; i < qtdPalavras; i++){
for(int j = i + 1; j < qtdPalavras; j++){
if(!strcmp(palavras[i].ptr, palavras[j].ptr)){
//printf("%s = %s \n", palavras[i].ptr, palavras[j].ptr);
palavras[j].ptr[0] = 0;
palavras[i].qtd += 1;
}
}
}
for(int i = 0; i < qtdPalavras; i++){
if(palavras[i].ptr[0] != 0){
printf("%s = %d\n", palavras[i].ptr, palavras[i].qtd);
}
}
fclose(arquivo);
//system("pause");
return 0;
}