C Program to sort names in alphabetical order is a c program to sort the given n numbers into alphabetical order from A to Z.

Strings in c
The string is a collection of characters. Remember string always ends with the null character.

Example :-
Names = sort, array, in , c ,programmimg
Sorted names = array, c, in, programming, sort

Algorithm to sort names in c

  1. Declare variables. n, str[]
  2. Take input names in an array. str[N]
  3. Traverse the array till end
    Compare array index values i.e previous and next index
    If i index name > i+1 index name
    Swap i index name and i+1 index name
    Else
    Continue
  4. Print array of names which are sorted

C Program to sort names in alphabetical order

#include<stdio.h>

#include<string.h>

main()

{

int i,j,n;

char str[100][100],s[100];

printf("Enter number of names \n");

scanf("%d",&n);

printf("Enter names in any order\n");

for(i=0;i<n;i++)

{

scanf("%s",str[i]);

}

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

if(strcmp(str[i],str[j])>0)

{

strcpy(s,str[i]);

strcpy(str[i],str[j]);

strcpy(str[j],s);

}

}

}

printf("\nThe sorted order of alphabets are:\n");

for(i=0;i<n;i++)

{

printf("%s\n",str[i]);

}

}

Output :

C Program to sort names in alphabetical order

Write A Comment