Hey I need some help with a school C++ problem

So we just started doing programming class at my school today and after learning the basics of C++ our teacher taught us the basics of strings, we did a few practice problems and they went fine. Then she had us write down a problem for homework, she spoke the thing she didn't write it anywhere so here it is word by word:

Write a program that reads from keyboard input a natural number n. There are n words separated by enter followed by a natural number k. n and k belong to the
[1 ; 20] interval, each word has a maximum of 20 lowercase characters. At least one of the words has exactly k amount of letters. The program has to build and display a string which contains the "read" words separated by a space in a way that all words with k letters will be displayed first and words with other amounts of letters displayed randomly afterwards.

I have absolutely no idea how to tackle this and the teacher will probably have my head. I mean where do the words come from? Is there some sort of function for strings that creates some form of random words within parameters? The teacher taught us strlen, strcat, strcopy, strstr and I don't see how I could use these.
Do you know what "natural number" means?

Do you know what "interval" means?

I mean where do the words come from?

If you're referring to the words above, they are common mathematical terms.

The teacher taught us strlen, strcat, strcopy, strstr and I don't see how I could use these.

Then you haven't studied strings, you've studied C-strings.


A natural number is a number belonging to the multitude of natural numbers {1, 2, 3,... n}

An interval is like when you have numbers between the 2 ends of the interval, sorry my english is not so good. So a square bracket interval of [1; 20] means all natural numbers between 1 and 20 including 1 and 20.

I mean where do the words that have to be displayed in the string come from?
I mean where do the words that have to be displayed in the string come from?

Probably from the console input.
Write a program that reads from keyboard input a natural number n.
But doesn't that mean the keyboard input is a number?
You probably need to read between the lines. Look at the next line of the assignment:
There are n words separated by enter followed by a natural number k.


So you would enter a value for n, which is the number of words which follow, then enter that number of words. And remember that one of those words the length of the string must match the value you enter for k.

So you would enter something like the following:
1
2
3
4
5
6
7
8
9
6
this
is
my
string
my
friend
5
The output, based on that input, should be something like:

string this is my my friend


So today at school the teacher told us she forgot to mention there was an <fstream> library and we had nothing to do with any words.

I managed to solve the thing with 6 lines of code because I only had to order them based on the 2 inputs n and k.

Thanks for the help though!
Topic archived. No new replies allowed.