C++ Program to Sort String

Google 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 Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs