Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The focus of this task is on identifying words that have the same letters as each other, but arranged in a different order. Some examples
The focus of this task is on identifying words that have the same letters as each other, but arranged
in a different order.
Some examples would be
lame and male
golf and flog
seat, east, sate and teas
Our task will be to read in a list of words, and then identify all of the groups of words that contain the
same letters. In order to break this task down into smaller components, we will first write a function
that checks whether two words have exactly the same letters or not Level Then in Level we will
use this function while we are examining all of the words in our list of words, in order to identify the
groups of words that have the same letters. Note that such a group can consist of more than two
words, as shown in the last example.
We have provided starting code inside the main.cpp program. You only need to complete the code
for main.cpp and submit that file.
Level weight:
For level you are required to complete the code for the function haveSameLetters. This
function takes in two strings as parameters, and should return true if the two strings contain
exactly the same letters as each other, and false otherwise.
Once you have implemented haveSameLetters, you can run the code in main.cpp that tests it
In order to run the code for level you need to pass in the value as a commandline argument. In
the main function, this will execute code that reads in two words from standard input, and prints
the value returned by haveSameLetters when called with those two words as parameters.
To test, you can simply type in the two words, or you can redirect your standard input from a text
file. For Level a text file may be more convenient
For example, if you enter as input
face cafe
the output should be
true
Level weight:
Once the basic function haveSameLetters has been completed, you can use it to accomplish the
task for Level To run Level you simply have to pass in something other than you could pass in
or you could simply pass in no commandline argument at all.
When the command line argument is not the code in main branches off to the else block.
This is where you need to place your code for Level 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 and it carries more weight than Level It may be easier
to solve the problem for just pairs of words, compared to groups of or more words. If you are
finding it difficult to solve this more challenging problem, you can still get partial credit for solving it
for examples where there are groups of two only. #include
#include
bool haveSameLettersconst std::string& str const std::string& str
add your code here
return true;
int mainint argc, char argv
char end;
long which ;
whichargc strtolargv &end, : ;
if which Level
std::string word word;
std::cin word;
std::cin word;
std::cout std::boolalpha haveSameLettersword word;
else Level
std::vector v;
std::string word;
while std::cin word
vpushbackword;
add your code here
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started