Sorting a list of Strings in Alphabetical order (C)

lleao picture lleao · Oct 14, 2016 · Viewed 21.3k times · Source

Ok, here is my problem. A teacher has to randomly select a student (from the students she has) to earn a special bonus in the final score and in order to do that she puts N pieces of paper numbered from 1 to N in a bag and randomly select a number K; the award-winning student was the K-th student in the student list. The problem is that the teacher does not know which number corresponds to which student because she lost the paper that contained this information. What she knows: the names of all students, and that, their numbers, from 1 to N, are assigned according to the alphabetical order.

So I need to get the set of names that is given as input, sort them alphabetically and then provide the name of the student who won the special bonus, but I'm having trouble doing so. The program I wrote orders all the names except the first.

In addition, the following warnings appear when I run the project with Code::Blocks:

  • (line 16) ISO C90 forbids array variable length 's' [-Wvla]
  • (Line 13) ISO C90 forbids mixed declarations and code [-Wpedantic]

Please, tell me what am I doing wrong here and if there's a better way to sort the names without having the specified amount of names.

Note: the program should stop reading the input when N and K are equal to zero.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n, k, i, j=0, aux, numMenorNome;
    char str[]="zzzzzzzzzzzzzzzzzzzz", str2[]="zwyxzzzzzzzzzzzzzzzz";

    do
    {
       scanf("%d%d", &n, &k);
       struct student
       {
           char nome[21]; /*name*/
           char nomes_ordenados[21]; /*array to put the names already sorted*/
       } s[n];

       for (i=0; i<n; i++)
       {
           scanf(" %s", s[i].nome);
       }

       for (i=0; i<n; i++)
       {
           aux = strcmp(str, s[i].nome); /*compares the string that would be the last in the alphabetical order ("zzzzzzzzzzzzzzzzzzzz") with the given names*/
           if(aux>0)
           {
               strcpy(str, s[i].nome); /*it gives me the name that comes first in alphabetical order */
               numMenorNome = i;  /* identification number of the name that was obtained */
           }
           if (i==(n-1))
           {
               strcpy(s[j].nomes_ordenados,str); 
               printf("%s\n", s[j].nomes_ordenados);
               strcpy(str, "zzzzzzzzzzzzzzzzzzzz"); 
               strcpy(s[numMenorNome].nome, str2); 
               j++;
               i=0; /* restarts the loop in order to obtain the second name in alphabetical order, the third name, the fourth name and so on */
               if(j==n)
                    break;
           }
       }
       printf("%s\n\n", s[k-1].nomes_ordenados);

    } while (n!=0&&k!=0);
    return 0;
}

Answer

4386427 picture 4386427 · Oct 14, 2016

Sorting an array of strings are real simple. Just use qsort and the existing compare function (i.e. strcmp)

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAMES 5
#define NAME_LEN 10

void print_names(char names[][NAME_LEN])
{
    int i;
    for(i=0; i<NAMES; ++i)
    {
        printf("%s\n", names[i]);
    }
}

int main(void) {
    char names[NAMES][NAME_LEN] = { "xxx", "uuu", "ccc", "aaa", "bbb" };

    print_names(names);
    printf("---------------------------------\n");

    qsort(names, NAMES, NAME_LEN, strcmp);

    print_names(names);

    return 0;
}