A
A
Andrey Dyrkov2015-11-15 14:32:59
.NET
Andrey Dyrkov, 2015-11-15 14:32:59

How to fill a line with words?

There is some word, for example "home".
It is necessary to fill a string with this word for a certain number of iterations (you can use not strings but a char array)
I tried to throw something in .. but it doesn’t work. the size of the iterations is greater than the characters in the string

string word = "home";
string result = "";
  for (int i = 0; i<19i++)
      {
         result += word[i];
       }

As a result, it should turn out like this
iterations 10
word home
homehomeho - result
Thanks in advance)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
RedHairOnMyHead, 2015-11-15
@ThePyzhov

Copies with a null character.
strcpy(result, word);

S
Sergey Vushnyakov, 2015-11-15
@leto2015

- Think about what you have here word[4]
- Calculate (find) the end of the line.
- Check the condition (and not the end of the line)
- If yes, then print the characters on a new one.
early please)

O
Oleg Tsilyurik, 2015-11-15
@Olej

for( int i = 0; i < rep; i++ )
   result += word[ i % word.length() ];

Of course, it's better to calculate word.length() in advance into an intermediate variable for efficiency, but maybe it will be clearer to you.
It would be better like this:
for( uint i = 0; i < rep; i += word.length() )
   result += rep - i > word.length() ? word : word.substr( 0, rep - i );

Or, if you like, like this:
for( uint i = 0; i < rep / word.length(); i++ )
   result += word;
result += word.substr( 0, rep % word.length() );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question