Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Level 2 ( weight: 7 5 % ) Once the basic function haveSameLetters has been completed, you can use it to accomplish the task for

Level 2(weight: 75%)
Once the basic function haveSameLetters has been completed, you can use it to accomplish the
task for Level 2. To run Level 2, you simply have to pass in something other than 1 you could pass in
2, or you could simply pass in no command-line argument at all.
When the command line argument is not 1, the code in main() branches off to the else block.
This is where you need to place your code for Level 2. We have already provided the first part: words
are read in from standard input, and they are stored one by one inside a vector (we are using the
Standard Template Library vector class for C++). In the next part of the code, you need to access
the words inside the vector, and print out all groups of words that have exactly the same letters.
Groups of words should be separated by commas (no spaces), and each group should appear on a
new line.
For example, if our input is
sore cake rose leaf teal fear late
Then the output should be
sore,rose
teal,late
(words should appear in the order in which they were entered into the vector.)
If the input is
earth heart hater atom moat mother
then the output should be
earth,heart,hater
atom,moat
Note that there are several tests in Level 2, and it carries mor
in this i got error please add expected ouput in that code
#include
#include
#include
bool haveSameLetters(const std::string& str1, const std::string& str2){
std::string sorted_str1= str1;
std::string sorted_str2= str2;
std::sort(sorted_str1.begin(), sorted_str1.end());
std::sort(sorted_str2.begin(), sorted_str2.end());
return sorted_str1== sorted_str2;
}
int main(int argc, char** argv){
char *end;
long which =0;
which =(argc >1)? strtol(argv[1], &end, 10) : 0;
if (which ==1){// Level 1
std::string word1, word2;
std::cin >> word1;
std::cin >> word2;
std::cout << std::boolalpha << haveSameLetters(word1, word2);
} else {// Level 2
std::vector v;
std::string word;
while (std::cin >> word){
v.push_back(word);
}
std::vector groups;
for (size_t i =0; i < v.size(); ++i){
std::string group = v[i];
for (size_t j = i +1; j < v.size(); ++j){
if (haveSameLetters(v[i], v[j])){
group +=","+ v[j];
v.erase(v.begin()+ j);
--j;
}
}
groups.push_back(group);
}
for (const auto& group : groups){
std::cout << group << std::endl;
}
}
return 0;
}
this is code and below is error
Input:
deeps speed
Expected output:
deeps,speed
Input:
star art rats
Expected output:
star,rats
Input:
star art rats tar
Expected output:
star,rats
art,tar
Input:
pig lag alp gal elk eke pal
Expected output:
lag,gal
alp,pal
Input:
pig lap lag alp gal elk eke pal
Expected output:
lap,alp,pal
lag,gal
Input:
meat
team
able
tame
bale
mate
lab
oh
ho
Expected output:
meat,team,tame,mate
able,bale
oh,ho
please solve this

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions