1- Input six names from text file and use strcomp and strcopy functions to sort 5 names (use cstring libtrary)
Where strcmp and strcpy are the followings:
strcomp(s1,s2): return 0 if s1 == s2
return positive number if s1>s2
return negative number if s1<s2
strcpy(s1,s2): s1=s2
using namespace std;
#include <stdio.h>
#include <cstring>
#define NO_OF_NAMES 6
main(void)
{
string s,Names[NO_OF_NAMES],s1,s2;
fstream infile;
int n=0;
string Name = "./Names.txt";
infile.open(Name.c_str());
if (!infile)
{
cout << "Unable to open file: "<<Name;
exit(1); // terminate with error
}
else
{
cout<<"\nAll Names from input file: "<<Name<<endl;
while(infile >> s)
{
if(n<NO_OF_NAMES) Names[n] = s;
cout<<s<<endl;
n++;
}
}
std::sort(Names, Names + NO_OF_NAMES, std::greater<string>());
cout<<"\nSorted Names: ";
for(n=NO_OF_NAMES-1;n>0;n--) cout<<Names[n]<<", ";
}
Comments
Leave a comment