Write a C++ program to sort different strings into alphabetic order, where strings are entered by user.
Program Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char str[4][30], temp[30];
int i, j;
cout<<"Enter any four string to sort in alphabetical order : ";
cout<<endl;
for(i=0; i<4; 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(temp, str[j-1]);
strcpy(str[j-1], str[j]);
strcpy(str[j], temp);
}
}
}
cout<<"Strings in alphabetical order are : ";
cout<<endl;
for(i=0; i<4; i++)
{
cout<<str[i];
cout<<endl;
}
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter any four string to sort in alphabetical order :
Rajput
Mukesh
Thakur
Suruchika
Strings in alphabetical order are :
Mukesh
Rajput
Suruchika
Thakur
Thanks
Mukesh Rajput
Post A Comment:
0 comments: