1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

How to arrange Names in Alphabetical order

Discussion in 'Microcontrollers' started by Parth86, Oct 21, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    538
    Likes:
    5
    I am looking help to arrange names in alphabetical order. first user check how many names are there and then arrange in alphabetical order

    I found this program this program arrange number in ASCENDING ORDER. I don't have idea how to do it for arranging name alphabetical order. What changes I have to do in program ?
    Code (c):

    #include <stdio.h>

    int main(void)
    {
        int a[10], i=0, j=0, n, t;

        printf ("\n Enter the no. of elements: ");
        scanf ("%d", &n);
        printf ("\n");

        for (i = 0; i <n; i++)
        {
            printf ("\n Enter the %dth element: ", (i+1));
            scanf ("%d", &a[i]);
        }

        for (j=0 ; j<(n-1) ; j++)
        {
            for (i=0 ; i<(n-1) ; i++)
            {
                if (a[i+1] < a[i])
                {
                    t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }
        }

        printf ("\n Ascending order: ");
        for (i=0 ; i<n ; i++)
        {
            printf (" %d", a[i]);
        }

          /* indicate successful completion */
          return 0;
    }
     
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Check to see if your compiler has qsort(); This is designed to do just this..
     
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    538
    Likes:
    5
    Is there any way to do it without qsort(); or with out string.h file ?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ramussons

    Ramussons Member

    Joined:
    May 15, 2013
    Messages:
    181
    Likes:
    13
    Location:
    Asia

    * Convert the Name(s) to an ASCII string.
    * Sort on the First ASCII character (which is a number)
    * Repeat for the 2nd, 3rd........
    That should do the sorting.
     
  6. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,985
    Likes:
    511
    Location:
    Cleveland, OH, USA
    ONLINE
    Don't upper and lower case create a problem? For example, del Castillo. Seems like you need a rule for name capitalization or merge the sets. The offset is 0x20 (.32) so there should be no ambiguity.

    John
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Yes! but whatever you come up with qsort() will do it much better.... As John has already said! You have to Uppercase everything, whilst remembering how they are initially.. You have to do each character... Take Justin and Justine for example... Two names that all the elements need checking...

    Minefield.... Someone has done the hard part!!
     

Share This Page