1
My activity is to read a sentence and ordered in alphabetical order, but is giving error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ordena(const void * a, const void * b)
{
return strcmp((char *)a, (char *)b);
}
int main(int argc, char** argv)
{
char **nomes, frase[900], *ponteiro;
nomes = malloc(sizeof(char*) * 3000);
int indice, cont = 0, i;
for(indice = 0; indice < 3000; indice++)
{
nomes[indice] = malloc(sizeof(char) * 100);
}
scanf("%[^\n]", frase);
ponteiro = strtok(frase, " ");
while(ponteiro != NULL)
{
strcpy(nomes[cont], ponteiro);
cont++;
ponteiro = strtok(NULL, " ");
}
qsort((void*)nomes, cont, sizeof(nomes[0]), ordena);
for(i = 0; i < cont; i++)
{
printf("%s\n", nomes[i]);
}
return 0;
}
Example of orange meat starter juice orange pickles orange pickles
Example of orange beef output pickles pickles juice
For me, your program generates as output
picles picles laranja laranja suco carne
. I couldn’t find where the mistake was.– Victor Stafusa