Answered step by step
Verified Expert Solution
Link Copied!

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 1). Then in Level 2, 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 1(weight: 25%)
For level 1, 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 1, you need to pass in the value 1 as a command-line 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 2, a text file may be more convenient).
For example, if you enter as input
face cafe
the output should be
true
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 more weight than Level 1. It may be easier
to solve the problem for just pairs of words, compared to groups of 3 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 haveSameLetters(const std::string& str1, const std::string& str2){
// add your code here
return true;
}
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);
}
// add your code here
}
return 0;
}

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

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

ISBN: 3540416641, 978-3540416647

More Books

Students also viewed these Databases questions