C Program to Sort Names in Alphabetical Order

By | 27.11.2016

Sort Names in Alphabetical Order


Write a C Program to Sort Names in Alphabetical Order. Here’s simple C Program to Sort Names in Alphabetical Order in C Programming Language.


Below is the source code for C Program to Sort Names in Alphabetical Order which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to Sort Names in Alphabetical Order  */

#include <stdio.h>
#include <string.h>
 
void main()
{
    char name[10][8], tname[10][8], temp[8];
    int i, j, n;
 
    printf("Enter the value of n \n");
    scanf("%d", &n);
    printf("Enter %d names n", \n);
    for (i = 0; i < n; i++)
    {
        scanf("%s", name[i]);
        strcpy(tname[i], name[i]);
    }
    for (i = 0; i < n - 1 ; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (strcmp(name[i], name[j]) > 0)
            {
                strcpy(temp, name[i]);
                strcpy(name[i], name[j]);
                strcpy(name[j], temp);
            }
        }
    }
    printf("\n----------------------------------------\n");
    printf("Input NamestSorted names\n");
    printf("------------------------------------------\n");
    for (i = 0; i < n; i++)
    {
        printf("%s\t\t%s\n", tname[i], name[i]);
    }
    printf("------------------------------------------\n");
}

Output:


/*  C Program to Sort Names in Alphabetical Order  */

Enter the value of n
4
Enter 4 names
merge
bubble
insertion
heap

----------------------------------------
Input Names    Sorted names
------------------------------------------
merge           bubble
bubble          heap
insertion       insertion
heap            merge  
------------------------------------------

Above is the source code for C Program to Sort Name in Alphabetical Order which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 1 vote
Article Rating
Category: C Programming Sorting Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments