C++ Program to Sort String
Advertisements
Sort String Program in C++
To sort strings in C++, We need to take two string from user side and sort them on the basis of alphabets.
Examples to Sort any String in C++
Here you can see first we enter any five name and sort these name according to alphabet.
Example
Faiz Komal Akash Shriniwaslu Harry Names After Sort Alphabetical Order are: Akash Faiz Komal Harry Shriniwaslu
To understand below example, you have must knowledge of following C++ programming topics; String in C++, for looping statements we need know For Loop in C++ and Array in C++.
C++ Program to Sort String
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[5][20], t[20];
int i, j;
cout<<"Please Enter Any Five Name: ";
for(i=0; i<5; i++)
{
cin>>str[i];
}
for(i=1; i<5; i++)
{
for(j=1; j<5; j++)
{
if(strcmp(str[j-1], str[j])>0)
{
strcpy(t, str[j-1]);
strcpy(str[j-1], str[j]);
strcpy(str[j], t);
}
}
}
cout<<"Names in Alphabetical Order are: \n";
for(i=0; i<5; i++)
{
cout<<str[i]<<"\n";
}
getch();
}
Output
Please Enter Any Five Name: Gaurav Varsha Sultan Uma Reetu Names in Alphabetical Order are: Gaurav Reetu Sultan Uma Varsha
Google Advertisment
