Sort a list of students by name in alphabetical order

1

That's what I thought:

Aluno *ordenarNome(Aluno *aluno){
    Aluno *aux;
    Aluno *aux2 = (Aluno*)malloc(sizeof(Aluno));
    Aluno *aux3 = (Aluno*)malloc(sizeof(Aluno));
    Aluno *aux4 = (Aluno*)malloc(sizeof(Aluno));
    Aluno *aux5 = (Aluno*)malloc(sizeof(Aluno));
    *aux = *aluno;
    char nome[30];
    int id = 0;
    int matricula;
    int idade;
    while(aux != NULL){
        *aux2 = *aux->prox;
        while(aux2 != NULL){
            if(aux2->nome[0] < aux->nome[0]){
                *aux3 = *aux2;
                id++;
            }
                aux2=aux2->prox;
        }
        if(id > 0){
            *aux4 = *aux;
            *aux = *aux3;
            while(aux4 != NULL){
                aux->prox=aux4;
                aux5 = aux4->prox;
                if(aux3->id == aux5->id){
                    break;
                }
                aux4=aux4->prox;
            }
        }
        id=0;
    aux=aux->prox;
    }
    aluno = aux;
    return aluno;
}

But it is failing (NOTE: compile using gcc in linux and gives that memory error)

    
asked by anonymous 19.05.2018 / 15:01

1 answer

1

Tried to use the strcmp () function of the string.h library?

The strcmp () function receives two strings to be compared to each other. The result can have 3 possible returns:

Less than 0 : Character of the first String is less than the second String.

Same as 0: When they are equal.

Greater than 0: When the character of the first String is larger (ASCII code) than the second String.

Syntax:

strcmp ( string1, string2 );

Small demonstration:

 #include <stdio.h>
 #include <string.h>//necessário para strcmp
  #include <conio.h>
   int main ()
  {
  char str1[3] = "abc";
  char str2[3] = "abd";
   int retorno;

    retorno = strcmp(str1, str2);
    printf("retorno = %d\n", retorno);
     //mostra o retorno da função strcmp

     getch();
     return 0;
     }
    
19.05.2018 / 15:12