C Examples

WAP in C to sort the names in a given list in ascending order

The Program in C to sort the names in a given list in ascending order is given below:

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

void bubbleSort(char names[][20], int n)
{
    int i, j;
    char temp[20];
    for (i = 0; i < n-1; i++)
    {
        for (j = 0; j < n-i-1; j++)
        {
            if (strcmp(names[j], names[j+1]) > 0)
            {
                strcpy(temp, names[j]);
                strcpy(names[j], names[j+1]);
                strcpy(names[j+1], temp);
            }
        }
    }
}

int main()
{
    char names[10][20];
    int i, n;

    // Input the number of names
    printf("Enter the number of names: ");
    scanf("%d", &n);

    // Input the names
    printf("Enter the names: \n");
    for (i = 0; i < n; i++)
    {
        scanf("%s", names[i]);
    }

    // Sort the names
    bubbleSort(names, n);

    // Print the sorted names
    printf("Sorted names: \n");
    for (i = 0; i < n; i++)
    {
        printf("%s\n", names[i]);
    }

    return 0;
}

Output:

Enter the number of names: 6
Enter the names:
DILLIRAM
JASHMAYA
KRISHNA
ARUNA
SRIJANA
KUMAR


Sorted names:
ARUNA
DILLIRAM
JASHMAYA
KRISHNA
KUMAR
SRIJANA

Pro-Tips💡

The program uses the bubble sort algorithm to sort the names in ascending order.

The function bubbleSort() takes an array of strings (i.e., char names[][20]) and its size as arguments, and sorts the array in ascending order using nested loops and the strcmp() and strcpy() string library functions to compare and swap names.

The main function first takes the number of names as input, then takes the names as input and stores them in an array.

Then it calls the bubbleSort() function to sort the names, and finally it prints the sorted names.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X