C Program to sort the names in alphabetic order using arrays


May 11, 2022, Learn eTutorial
1592

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is the strcmp function in C?

In this c program, we need to enter the names into an array and sort that names in alphabetical order. For achieving this, we are using two functions, 'strcmp' and 'strcpy,' so that we can do it very quickly. Strcmp is called a string compare function, which is for comparing two strings. The 'strcmp()' returns the value less than zero if string1 is greater than string2. Else strcmp returns a greater than zero value.

What is strcmpi and strcpy function do?

Strcmpi() function is used here in this c program for ignoring the case of the letters. Strcpy() is known as a string copy function, as the name suggests it copy a string into another variable. Here we store the names into an array. Before starting the for loop, we copy the name into a temporary variable. Inside for loop, we compare each term with the next name then sort it by interchange the terms using strcpy. For using the strcmp, strcpy functions, we have to include the header file 'string.h'

How do sort the names alphabetically?

In this c program, we have to sort the names in alphabetical order. We have to accept the number from the user for how many terms the user wants to sort. The count of terms or names the user wants to sort into the variable n.Then will receive the words from the user and store it in two arrays.

One is a temporary array named as tname.Now both the arrays 'name[][]',and 'tname[][]' contains the names.Using a for loop, we have to compare the name in the array 'name[][]' with the next name of the same array. For comparing the names, we are using the 'strcmp()' function in c.

If the first name is greater than the second, we interchange the first name with the second one and continue the for loop. After the first iteration, we will get the sorted name in the first position of the 'name[][]' array the for loop continues until it checks all names. Now the 'name[][]' array contains the sorted names and 'tname[][]' contains the input names.Then we will display the input names and sorted names properly.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions into the C program.

STEP 2: Include the Header file 'string.h' for doing String operations like strcmp,strcpy.

STEP 3: Initialize two-character arrays named 'Tname,' 'name,' and a String temp.

STEP 4: Initialize the integer variable N to accept the Number of names the user wants to Sort.

STEP 5: Accept the Number of names the user wants to sort into N.

STEP 6: Accept the names from the user into the Array 'name[][]', then copy the name to 'Tname[][]' by using 'strcpy()' function.

STEP 7: Repeat step 6 until we accept all names from the user.

STEP 8: Using For loop, compare each name in the Array 'name[][]' to the next word in the same Array. We are using the 'strcmp()' Function to compare the terms.

STEP 9: If the 'strcmp()' Function's return value is Greater than Zero, we will interchange the names.

STEP 10: Then, repeat step 8 until we compare all the names.

STEP 11: Display the Input Names as the elements in the 'Tname[][]' Array.

STEP 12: Display the Sorted names as the elements in the 'name[][]' Array.

C Source Code

                                          #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); /* gets the names into a array and copy it into temporary array*/
  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) /* compare and interchange each name to get sorted in the alphabetic order */ {
        strcpy(temp, name[i]);
        strcpy(name[i], name[j]);
        strcpy(name[j], temp);
      }
    }
  }
  printf("\n----------------------------------------\n"); /* prints the names in the alphabetic order */
  printf("Input Names\tSorted names\n");
  printf("------------------------------------------\n");
  for (i = 0; i < N; i++) {
    printf("%s\t\t%s\n", Tname[i], name[i]);
  }
  printf("------------------------------------------\n");
} /* End of main() */
                                      

OUTPUT

Enter the value of N
3

Enter 3 names
Monica
Lakshmi
Aron

----------------------------------------
Input Names     Sorted names
----------------------------------------
Monica          Aron

Lakshmi        Lakshmi

Aron              Monica