Salve ragazzi, ho un problema con questa funzione che dovrebbe oltre a contare le parole distinte di una stringa anche stamparle. Premetto che sono alle prime armi e non riesco proprio a capire dov'è l'errore.
codice:#include <stdio.h> #include <stdlib.h> #include <string.h> #define DIM 100 int contaParoleDistinte(char[], char*[]); int main(int argc, char *argv[]) { char s[DIM], *token[DIM]; int i; printf("Inserire una stringa: (max %d caratteri) ", DIM); gets(s); printf("La stringa e\' costituita da %d parole distinte\n", contaParoleDistinte(s, token)); printf("Le parole distinte individuate sono:\n"); for(i=0; i<contaParoleDistinte(s, token); i++){ puts(token[i]); } system("pause"); return 0; } int contaParoleDistinte(char s[], char *token[]){ char *ptr; int cont=0, i, trovato; ptr=strtok(s, " "); token[cont]=malloc((strlen(ptr)+1)*sizeof(char)); strcpy(token[cont],ptr); cont++; while(ptr!=NULL){ for(i=0; i<cont; i++){ if(strcmp(token[i], ptr)==0) { trovato=1; break; } else trovato=0;} if(trovato=0){ token[cont]=malloc((strlen(ptr)+1)*sizeof(char)); strcpy(token[cont],ptr); cont++; } ptr=strtok(NULL, " "); } return cont; }

Rispondi quotando


