Determining the length of any string without a pre-selected size of the array?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
19 views
Hi, is it possible to calculate the length of the string in advance is not limited ?there is a program which chooses the longest line entered by the user via the console. now it is limited for example to 100 characters. how to dynamically change the size of the array or how it's done ?
by | 19 views

1 Answer

0 like 0 dislike
Dynamically allocate initial memory size, read in it the specified number of characters (the buffer size), if the end of the line was not doing resize the buffer, read, check the end of the row, etc.
You can read 1 character and immediately his check for the end of the line, but the memory at the same time need to increase if necessary. Slower most likely not, because the standard library does buffering by default, i.e. will still be read by 1 byte.
by

Related questions

0 like 0 dislike
1 answer
asked Jun 12, 2019 by AleksandrB
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 10, 2019 by dandropov95
110,608 questions
257,187 answers
0 comments
40,796 users