In this program we are going to Sort the String in Alphabetical order using c++ programming language.
Program:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[5][20],t[20];
int i,j;
cout<<"Enter the input string:\n";
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<<"Strings in alphabetical order:\n\n";
for(i=0;i<5;i++)
{
cout<<str[i]<<"\n";
}
return 0;
}
OUTPUT:
BY
REGU RAM SV.
good
ReplyDeletePost a Comment