user2046229
user2046229

Reputation: 47

Sorting an array alphabetically in the C programming language?

I would like to sort an array alphabetically that looks like this:

I have tried various methods, but still no way. it crashes, and no idea yet why. Would you have some tips to start with this issue?

Since that I am not so much expert, the code is aimed at being simple (to read/understand) and doing the job.

thx, Regards

/* note: datalist[...] is basically the ouput from a sort of ls (dir
read) that it unsorted. So that: datalist[0] is ".." datalist[1] is
"file2.c" datalist[2] is "file34.c" and so on.*/


    char datalist[500][2024] ; 


void sortData(int aoptiondt)  {   ///////////////////////////LOCAL
DECLARATIONS/////////   int MAX = 500 ;    int current; int walker;  
int smallestIndex;   char* temp;

  ///////////////////////////DEFINITIONS//////////////////



    for (current = 0; current < MAX - 1; current++)
    {
      smallestIndex = current;
      for (walker = current; walker < MAX ; walker ++)
      {
        if (strcmp(datalist[walker], datalist[smallestIndex]) < 0)
          smallestIndex = walker;
      } //for walker

      //Swap to position smallest at what is the current position
      strncpy( temp , datalist[current] , PATH_MAX);
      strncpy( datalist[current] , datalist[smallestIndex] , PATH_MAX);
      strncpy( datalist[smallestIndex] , temp, PATH_MAX);
    } //for current    }

  return; }


//blabla
    int  main() {



    }

Upvotes: 1

Views: 2876

Answers (3)

One Man Crew
One Man Crew

Reputation: 9578

You don't just need an array of characters, you need an array of character arrays. As usual with C, you need to be careful about memory management, and staying with the bounds of your allocated arrays. See below for my solution. I've used a simple selection sort algorithm, and it works perfectly well for an application such as this. Here's how it's done:

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

#define N 5
#define MAX_STRLEN 80

void sort(char **a, int n);
char *stringArray[N];

int main(int argc, char *argv[]) 
{
   int i;

   printf("\nEnter %d names, one per line:\n",N);
   for (i = 0; i < N; i++) 
   {
      stringArray[i] = (char *)malloc(MAX_STRLEN);
      strcpy(stringArray[i],"");
      printf("> ");
      fgets(stringArray[i],MAX_STRLEN,stdin);
      *strchr(stringArray[i],'\n') = '\0';
   }
   sort(stringArray,N);
   printf("\nSorted:\n");
   for (i = 0; i < N; i++) 
   {
      puts(stringArray[i]);
      free(stringArray[i]);
   }

   return 0;
}

/* selection sort */
void sort(char **a, int n) 
{
   int min,i,j;
   char t[MAX_STRLEN];

   for (i = 0; i < n; i++) 
   {
      min = i;
      for (j = i+1; j < n; j++) 
      {
        if (strcmp(a[j],a[min]) < 0) 
          min = j;
      }
      strcpy(t,a[min]);
      strcpy(a[min],a[i]);
      strcpy(a[i],t);
   }
}

Upvotes: 1

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11267

As always - qsort is your best friend:

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

#define LENGTH 6
#define STRING_SIZE 4
#define STRINGS "eed", "abd", "cde", "abc", "acd", "ade"

char strUnsorted[][STRING_SIZE] = {STRINGS};
char strSorted[][STRING_SIZE]   = {STRINGS};

int compare (const void * a, const void * b) {
  return strcmp(a, b);
}

int main () {

  qsort(strSorted, LENGTH, STRING_SIZE, compare);

  printf("Unsorted | Sorted\n");
  printf("-----------------\n");

  int i;
  for (i=0; i < LENGTH; i++)
    printf ("  %s    |   %s\n", strUnsorted[i] ,strSorted[i]);

  return 0;
}

Upvotes: 2

bmavus
bmavus

Reputation: 892

http://www.asciitable.com/index/asciifull.gif here is an ascii table. if you look at this, you will see character comes in a sort. this meanyou can cast char to int and get its ascii number then you can use this number to sort them.

A sample:

  int len =strlen(word);
  int wordWeight=0;
  for(int i=0;i<len;i++)
  {
      wordWeight+=(int)word[i];
      wordWeight*=1000;
  }

by using wordWeight youcan sort them. NOTE: we are multipling ascii values to 1000, because ascii values changes between 0 and 255 but you will have problem if some words start uppercase and others start lovercase. But i think you can handle it

Upvotes: -1

Related Questions