0

Hi everyone if you can help.me understand this code in c++

https://code.sololearn.com/crqe3xL0GqC2/?ref=app Its the n-1 part why n-1 I'm kind of confused.

8th Dec 2020, 5:07 AM
boba
boba - avatar
2 Respuestas
+ 1
cuz the while loop is counting backwards. also you can also write "n = n - 1" as "n--" or "n -= 1"
8th Dec 2020, 5:22 AM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 1
This function seems to add the substring of a given string to itself, and does so on each iteration. The value of n decreases by 1 after each iteration, so as you approach the end, the resulting substring being added to w gets shorter. As for why it is n-1, that really depends on what you are trying to achieve with the function. You can handtrace it to understand how it works, for example, calling the function with word = "hello" and n = 5 yields the following result: repeatFront("hello", 5) hello + w.substr(0, 4) hello + hell + w.substr(0, 3) hello + hell + hel + w.substr(0, 2) hello + hell + hel + he + w.substr(0,1) hello + hell + hel + he + h hellohellhelheh
8th Dec 2020, 5:31 AM
Fermi
Fermi - avatar