.length in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

.length in c++?

how to fix this? https://code.sololearn.com/cM7s7U5D9hz4/?ref=app I want to *Output* "aaa bbb ccc ddd eee fff" without using i<6, in java they use i<z.length https://code.sololearn.com/c1qDoEd8ds04/?ref=app

15th Mar 2020, 10:37 AM
Zai Santillan
Zai Santillan - avatar
2 Answers
+ 4
i<(int)(sizeof(z)/sizeof(string)) because sizeof gives you the size of the array in bytes and you have to divide by the memory size of each element, that is sizeof(string) But even easier is to use i < (int) size(z)
15th Mar 2020, 10:46 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Do like this i < (int) (sizeof(z)/sizeof(z[0])) sizeof (z) gives size in bytes.
15th Mar 2020, 10:47 AM
A͢J
A͢J - avatar