Ciao a tutti,devo fare un programma nel quale un utente inserisce delle parole e il programma le restituisce in output ordinate lessicograficamente.
Questo è il codice:
codice:
int main(int argc, char* argv[]){
char **stringa;
char swap[15];
int i=0,c,j,cont,k;
printf("Quante parole vuoi inserire?");
scanf("%d",&cont);
stringa = (char**) malloc(sizeof(char*) * cont);
for(k=0;k<cont;k++){
printf("inserire parola: ");
scanf("%s",&swap);
stringa[k] = (char*) malloc(sizeof(char) * strlen(swap));
strcpy(stringa[k],swap);
}
while(stringa[i+1]){
j=i;
c = strcmp(stringa[i],stringa[i+1]);
if( c >= 0) {
strcpy(swap,stringa[i]);
strcpy(stringa[i],stringa[i+1]);
strcpy(stringa[i+1],swap);
while(j>0){
c = strcmp(stringa[j-1],stringa[j]);
if(c >= 0){
strcpy(swap,stringa[j-1]);
strcpy(stringa[j-1],stringa[j]);
strcpy(stringa[j],swap);
j--;
}
else j--;
}
i++;
}
else i++;
}
}
Quello che non capisco è perchè se creo un vettore dove inserisco io le parole funziona,se invece le faccio inserire all'utente no,mi da un problema di Stackdump.
Grazie