How can i keep the length of the string in a variable?
for exemple:
strlen(name);
And then i want to save that number. how can i do that?
How can i keep the length of the string in a variable?
for exemple:
strlen(name);
And then i want to save that number. how can i do that?
char buffer[] = "hello";
int length = strlen(buffer);
That will give you the string length minus the terminating zero at runtime.
If you wish to know the buffers length you must add one.
It can also be calculated at compile time and made a const variable,
#define COUNT_ENTRIES(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
char buffer[] = "hello";
const size_t BUFFER_LENGTH = COUNT_ENTRIES(buffer);
If you are not familiar with the idea of declaring variables and stuffing values in them, you need a basic primer on the C/C++ language. Doing a beginner C tutorial will get you much further along than attempting to code by cookbook.
Good points, Paul and Lloyd. My answer was too brief. I just take it as given that 'strlen()' doesn't include the terminating null character, and also should have addressed the real problem - the fact that he didn't understand how to assign it's result to a variable.
@leatavsan, you'll find plenty of info on basic C++ programming here:-
C++ Language
And a good reference to the standard C++ libraries here:-
Standard C++ Library reference
Also, 'strlen()' is here:-
strlen()