V
V
Vadim kyklaed2018-05-06 19:19:26
C++ / C#
Vadim kyklaed, 2018-05-06 19:19:26

Determining the length of any string, without a predetermined array size?

Hello, tell me, is it possible to calculate the length of a string that is not limited in advance? There is a program that selects the longest string from the user entered through the console. now it is limited to eg 100 characters. how to dynamically change the size of an array or how is it done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-05-06
@kyklaed

You dynamically allocate the initial memory size, read the specified number of characters into it (according to the size of the buffer), if there was no end of the line, resize the buffer, read it out, check the end of the line, etc.
You can read 1 character at a time and immediately check it for the end of the line, but the memory must also be increased if necessary. Most likely it will not be slower, because. the standard library does buffering by default, i.e. actually all the same will be read not on 1 byte.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question