In: Computer Science
Asks the user "How many parrots do you own?"
2: Reads in that many single word names (one for each parrots) from the user and stores the names in an array.
3: After the user has finished entering names, the program should display the list of all the parrots the user entered.
4: then display the parrots in alphabetical order for part 3. Note: string objects can be compared with the “<” operator, which will determine if a given string is alphabetically less than another string.
You may assume the user will own at most 50 parrots.
c++
Solution
Code
#include<bits/stdc++.h>
using namespace std;
bool compareparrots(string a,string b)
{
return a<b;
}
int main()
{
cout<<"How many parrots do you own? ";
int n;
string parrot[50];
cin>>n;
for(int i=0;i<n;i++)
cin>>parrot[i];
cout<<"User owned parrots are:"<<endl;
for(int i=0;i<n;i++)
{
cout<<parrot[i];
cout<<endl;
}
sort(parrot,parrot+n,compareparrots);
cout<<"Parrots in alphabetical order: "<<endl;
for(int i=0;i<n;i++)
{
cout<<parrot[i]<<endl;
}
}
Screenshot
Output
---
simplified the code and given
if you have any doubts, question, let me know, love to help
all the best