358 points
19 13 11

Sometimes, we need to sort different data in alphabetical order. In this post, we are going to see how to sort names in alphabetical order in C++.

Sort Names in Alphabetical Order in C++:

In order to sort the names in alphabetical order, we need to use the strcmp() function of the String class. Here is the definition and syntax for using this function:

strcmp() Function:

The strcmp() function is used to compare the first character of two given strings. The syntax for using this function is as follows:
strcmp(const char* left-hand-side, const char* right-hand-side);

This function returns:

  • positive number if a certain character in LHS is greater than the corresponding character in rhs.
  • negative number if a certain character in lhs is lesser than the corresponding character in rhs.
  • zero if the corresponding characters are the same in rhs and lhs.
    Now to arrange the names in alphabetical order, we need to check whether the first character in each name appears before or after the corresponding character in another name. Here is the program that performs the desired task:
    (Refer to the comments to understand code)

     using namespace std;
     int main()
         char names[3][10], temp[10];   //declaring char arrays to conveniently take input from users
         int i, j;    //loop variables
         for(i=0; i<3; i++) //loop to take input
             cout<<"Enter a Name : ";
         for(i=1; i<3; i++)    //loop to swap the names based on their first letter
                 for(j=1; j<3; j++)
                         if(strcmp(names[j-1], names[j])>0)  //swapping the names if 1st character of each name is greater than the first letter of another name
                                 strcpy(temp, names[j-1]);
                                 strcpy(names[j-1], names[j]);
                                 strcpy(names[j], temp);
         cout<<"\n Names After Sorting:\n";
         for(i=0; i<3; i++)   //printing the names after sorting
                 cout<<" ";
     return 0;



Enter a Name : 
Enter a Name : 
Enter a Name : 
Names After Sorting:

1 Comment

Sorting a word in c ++ is called sorting. You have worked hard to explain.

More Posts

how to sort a string in java Hasnain_khan - Oct 13, 2020
Java programs to sort array in descending order. Being human - Jun 2
Write a program to accept a sentence from the user and print each word in a new line in c sakshi - Aug 28
print the two strings in alphabetical order. assume the strings are lowercase. end with newline. SharadMagar450 - Aug 12
What is the number of swaps required to sort n elements using selection sort in the Worst Case sakshi - Sep 16
How to sort array of objects in javascript IvanLomakin - Aug 4
How to sort a linked list in java without using collections kesav.eee - Jun 15
Sort the array according to frequency of elements kesav.eee - Aug 12
How to calculate length of string in C hhh98hd - Aug 3
How to convert a string to integer in c++ amna - Jul 3