C++ Program to Sort String

Advertisements

Prev Tutorial Next Tutorial

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
c++ program to sort strings
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements