# Sort ascending n strings in C

**URL:** <https://community.unix.com/t/sort-ascending-n-strings-in-c/263066>\
**Category:** Programming\
**Created:** [April 2, 2010, 7:34am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066 "2010-04-02T07:34:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![1\_0](https://community.unix.com/letter_avatar/1_0/32/5_5575768a8748004e209b776fc1b2916d.png) [@1\_0](https://community.unix.com/u/1_0)\
**Post date:** [April 2, 2010, 7:34am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/1 "2010-04-02T07:34:26Z")

</div>

Hy guys. My English is not so good, sorry for any mistakes.  
I'm a bigginer in C, and I have a problem. I want to sort ascending n strings, but I can't read the strings. Here is what I've done so far:

```nohighlight
//sort ascending n strings

#include <stdio.h>

int main()
{
  int n,i,j;
  char a[20];
  printf("How many strings?");
  scanf("%d",&n);
  
  for (i=1;i<=n;i++)
  {
    printf("a[%d]=",i);
    scanf("%s",&a);
  }
    
  printf("\n");
  
  for (j=1;j<=n;j++) //Here I want to write them, so I'm sure I've read them good
    printf("%s\n",a[j]); //the problem apears here: Segmentation fault
    
  return 0;
}

```

If someone could help me, I would be grateful. :b:  
ps: I have read in this forum something about sscanf(), gets(), but I don't know how to use them... How do I use them in this problem?

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [April 2, 2010, 7:50am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/2 "2010-04-02T07:50:39Z")

</div>

I sure hope this is not homework -

```nohighlight
#include <stdio.h>

int main(int argc, char **argv)
{
  int n=0,
      i=0,
      j=0;
  char a[20][80]={0x0}; /* this makes twenty 80 character-long strings */
  printf("How many strings?");
  scanf("%d",&n);  

  for (i=1;i<=n && i < 20;i++)
  {
    printf("a[%d]=",i);
    scanf("%s", a);
  }

  printf("\n");

  for (j=1;j<=n && j < 20;j++) //Here I want to write them, so I'm sure I've read them good
    printf("%s\n",a[j]); //the problem apears here: Segmentation fault - do not go past 20

  return 0;
}

```

I made some changes. You still need a sorting algorithm.

---

<div class="post-metadata">

**Author:** ![1\_0](https://community.unix.com/letter_avatar/1_0/32/5_5575768a8748004e209b776fc1b2916d.png) [@1\_0](https://community.unix.com/u/1_0)\
**Post date:** [April 2, 2010, 11:39am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/3 "2010-04-02T11:39:40Z")

</div>

Not homework, I do this for fun, but sometimes it doesn't turn out like I want. I'm going to look over the changes you made, and learn from my mistake 🙂 Thank you jim mcnamara. :b:  
By the way, it's most likely that I would make some mistakes on the sorting algorithm.  
To sort 2 strings, I have to break down intro chars every string and compare the 2 chars?  
String 1: abc  
String 2: abd

a==a, b==b ,c!=d, resuts string 1\<string 2. Something like that?

---------- Post updated at 10:39 AM ---------- Previous update was at 07:24 AM ----------

I made a sorting algorithm for 2 strings:

```nohighlight
//sort ascending two strings

#include <stdio.h>

int main()
{
  char a[10],b[10];
  char s1,s2;
  int i=0;
  
  printf("First string:"); scanf("%s",&a);
  printf("Second string:");scanf("%s",&b);
  printf("\n");
  printf("String 1:%s\n",a);
  printf("String2:%s\n\n",b);
  printf("Ascending order:\n");
  
  do
  {
    s1=a;
    s2=b;
    i++;
  }
  while (s1==s2);
  
  if (s1<s2)
  {
    printf("%s ",a);
    printf("%s",b);
  }
  else
  {
    printf("%s ",b);
    printf("%s",a);
  }
  printf("\n\n");
  return 0;
}

```

Then I tryed to apply it to my problem.

```nohighlight
//sort ascending n strings
#include <stdio.h>

int main(int argc, char **argv) //why int argc, char**argv ?
{
  int n=0,
      i=0,
      j=0,
      l=0;
  char s1,s2;
  char a[20][80]={0x0}; //what does {0x0} means?
  char aux[80];
  
  printf("How many strings?");
  scanf("%d",&n);  

  for (i=1;i<=n && i < 20;i++)
  {
    printf("a[%d]=",i);
    scanf("%s", a);
  }

  printf("\n");

  for (j=1;j<=n && j < 20;j++)     
    printf("%s\n",a[j]);     

  for (i=1;i<=n-1 && i<20;i++) //a and a[i+1] so i goes to i<=n-1 ?
  {
    do
    {
      s1=a[l];
      s2=a[i+1][l];
      l++;
    }
    while(s1==s2);
    
    if (s1<s2)
    {
      aux=a; //invalid array assignment
      a=a[i+1]; //invalid array assignment
      a[i+1]=aux; //invalid array assignment
    }
  }
    
  for (j=1;j<=n && j < 20;j++)        
    printf("%s\n",a[j]);    
    
  return 0;
}

```

It has 3 errors. I declared wrong the aux variable, yes? How can I interchange the 2 strings compared?

---

<div class="post-metadata">

**Author:** ![anon57720281](https://community.unix.com/letter_avatar/anon57720281/32/5_5575768a8748004e209b776fc1b2916d.png) [@anon57720281](https://community.unix.com/u/anon57720281)\
**Post date:** [April 2, 2010, 1:03pm UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/4 "2010-04-02T13:03:13Z")

</div>

part of the art of programming is to use libraries.  
try using qsort from stdlib.h

---

<div class="post-metadata">

**Author:** ![1\_0](https://community.unix.com/letter_avatar/1_0/32/5_5575768a8748004e209b776fc1b2916d.png) [@1\_0](https://community.unix.com/u/1_0)\
**Post date:** [April 2, 2010, 6:17pm UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/5 "2010-04-02T18:17:32Z")

</div>

I'll try. I have never used qsort, but i'll google it and come back with new questions 🙂

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [April 4, 2010, 9:51am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/6 "2010-04-04T09:51:44Z")

</div>

Hi.

> [@1/0](#):
>
> I'll try. I have never used qsort, but i'll google it and come back with new questions 🙂

Meta-help:

There are man pages available here at [unix.com](http://unix.com). I would not discourage your use of Google, but when you know the name of an item, such as qsort, you might save some time by looking here.

Click the main-menu-bar item Man Pages, enter a string, check the appropriate controls, and you will see (for qsort and apropos) a number of choices.

Good luck ... cheers, drl

---

<div class="post-metadata">

**Author:** ![1\_0](https://community.unix.com/letter_avatar/1_0/32/5_5575768a8748004e209b776fc1b2916d.png) [@1\_0](https://community.unix.com/u/1_0)\
**Post date:** [April 4, 2010, 10:21am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/7 "2010-04-04T10:21:54Z")

</div>

Thank you. I didn't know that until now. It sure is useful. I only need the sorting algorithm now, but it is giving me problems. Sorry for the english... some words might be writen wrong (I'm romanian).

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [April 4, 2010, 10:58am UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/8 "2010-04-04T10:58:18Z")

</div>

Hi.

> [@1/0](#):
>
> Thank you. I didn't know that until now. It sure is useful. I only need the sorting algorithm now, but it is giving me problems. Sorry for the english... some words might be writen wrong (I'm romanian).

You are welcome. I think your English is very good, certainly far better than my Romanian would be (a sister-in-law of mine is of Romanian descent. Her father has a strong accent, but I have only heard him speak English. We call him Mike, and I recall that he was named after King Michael. But I digress.)

If you mean you are looking for a description of qsort, see [Quicksort - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Quicksort) I usually start with man pages, then Wikipedia, then Google.

Best of luck ... cheers, drl

---

<div class="post-metadata">

**Author:** ![1\_0](https://community.unix.com/letter_avatar/1_0/32/5_5575768a8748004e209b776fc1b2916d.png) [@1\_0](https://community.unix.com/u/1_0)\
**Post date:** [April 4, 2010, 12:14pm UTC](https://community.unix.com/t/sort-ascending-n-strings-in-c/263066/9 "2010-04-04T12:14:20Z")

</div>

I try to write as good as possible, after all, this is an English forum.

I'm having problems understanding how qsort works. Pointers, and functions aren't new to me, but I don't master them well, still having difficulties using the right method. I'm trying to solve this problem for a mounth now, still no succes, but with more reading, and applying on smaller problems, I will solve this one too. 🙂
