#include <stdio.h> #include <string.h> #include <stdlib.h> struct no_lista { struct no_lista *prox; char palavra[50]; int cont; }; struct no_lista *novo(struct no_lista *prox, char palavra[]) { struct no_lista *n = malloc(sizeof(struct no_lista)); strcpy(n->palavra, palavra); n->cont = 1; n->prox = prox; return n; } int busca(char *palavra, struct no_lista *lista) { for (; lista != NULL; lista = lista->prox) { if (strcmp(palavra, lista->palavra) == 0) { lista->cont++; return 1; } } return 0; } int main(int argc, char *argv[]) { FILE *arquivo = fopen(argv[1], "r"); struct no_lista *lista = NULL; char palavra[50]; while(!feof(arquivo)) { fscanf(arquivo, "%s", palavra); if (busca(palavra, lista) == 0) { lista = novo(lista, palavra); } } for (; lista != NULL; lista = lista->prox) { if (lista->cont > 1) printf("%s %d\n", lista->palavra, lista->cont); } return 0; }
Date: 2010/11/18 14:32:45