Write a program to arrange fruits names in ascending order. Input will be given by user. Use array to perform the given task.
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int i, j;
int n;
cout<<"Enter number of fruits: ";
cin>>n;
char name[n][30], temp[20];
cout<<"\nEnter fruit names : "<<endl;
for(i=0; i<n; i++)
{
cout<<" ";
cin>>name[i];
}
for(i=1; i<n; i++)
{
for(j=1; j<n; j++)
{
if(strcmp(name[j-1], name[j])>0)
{
strcpy(temp, name[j-1]);
strcpy(name[j-1], name[j]);
strcpy(name[j], temp);
}
}
}
cout<<"\nFruit Names Sorted in Alphabetical Order : "<<endl;
for(i=0; i<n; i++)
{
cout<<name[i]<<endl;
}
return 0;
}
Comments
Leave a comment