Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 38: Write a C++ program to arrange the no of words given by user (using bubble sort).

#include<iostream>
#include<string.h>
#include<string.h>
using namespace std;

int main()
{
	int n;
	cout<<"\nenter the no of strings : ";
	cin>>n;
	string str[n];
	cout<<"\nenter the strigs :\n";

	for(int i=0;i<n;i++)
	{
		cin>>str[i];
	}

	cout<<"\nstrings after sorting : ";
	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<n-i-1;j++)
		{
			if(str[j].compare(str[j+1])>0)
			{
				string temp;
				temp=str[j];
				str[j]=str[j+1];
				str[j+1]=temp;
			}
		}
	}

	for(int i=0;i<n;i++)
	{
		cout<<"\n"<<str[i];
	}
	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.