Strlen() Function

Well...I know how this works but I still have a question about what the author said in the book(instruction book).

Well...Usually you use strlen() function to get the length of a string. so if for instance, you type in the name, let say, basicman, because the name basicman contains eight letters, and thus it returns the value of 8.

Here is the code:
and assuming everything is properly written out with all directives and header files. And the user input basicman via the input facility of CIN.

1
2
3
4
5
6
7
8
9
10
int main()
{
    using namespace std;
    const in Size = 15;
    char name1[Size];

...

cout << strlen(name1) << " Letters.\n";
}


That's what the author states in the book, ..."strlen() function returns the size of the string stored in the array and not the size of the array itself. Also strlen() counts just the visible characters and not the null character. Thus it returns a value of 8 not 9, for the length of basicman. If cosmic is a string, the minimum array size for holding that string is str(cosmic) + 1."

I am just a bit confused about what he meant in the last sentence about str(cosmic) + 1. Does he mean that the minimum array size for holding the string is the length of a string, provided by the user or the programmer himself, + 1? Well...if we wanna get the minimum array size we could use sizeof operator. Why the use of strlen() function?
Within the array holding the string, there is an additional special character in the next element after the string that is there to signal the end of a valid sequence. It's the null charater, which is written as '\0'.

So if you have a string that says "hello world", within the array it would look like:

'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '\0'

Now the array holding these characters could be much bigger than needed for the letters, so that last null character is there to determine the end of the valid string sequence. So strlen() returns the size of the string, not including the null character.

As for sizeof, that returns actual size in bytes of the variable itself, as opposed to number of elements within that variable.
Last edited on
Yeah, the actual size of the array holding the characters could be very long, the null character '\0' acts as a signal to terminate the string, so the "things" after that would be not considered part of the string.

http://www.cplusplus.com/forum/beginner/7947/
Last edited on
let's say you want to store "cosmic" in the string strAstro.

Since you know 'cosmic' is exactly 6 characters long, you would be tempted to do this:
char strAstro[6];

Though the compiler accepts this, it is wrong.
Character strings are always stored with a null character ( '\0' ) appended immediately where the string ends.

So let's say initially your array is declared to hold 10 characters:

1
2
char strAstro[10];
cin >> strAstro;        //suppose the user will input cosmic here 


Each element in array strAstro will be:

strAstro[0] : c
strAstro[1] : o
strAstro[2] : s
strAstro[3] : m
strAstro[4] : i
strAstro[5] : c
strAstro[6] : \0
strAstro[7] :
strAstro[8] :
strAstro[9] :

Now as you see, the last three bytes remain empty. This is alright as these bytes are reserved for that array (since you declared strAstro to be 10 elements long initially).
Note the presence of the terminating null character.

strlen() returns the number of characters in the array, excluding null character. So if we passed strAstro to it, it would return 6.

So if you know the exact number of characters that you want to store in your array (in our case 6 for cosmic), you should initialise an array of length strlen(theString) + 1 (and thereby avoid the excess empty bytes as in the example above.

But suppose we had declared strAstro as:

1
2
char strAstro[6];
cin >> strAstro;  //user inputs 'cosmic' here 


The computer will store the first 6 bytes in the space allocated for the array like this:

1
2
3
4
5
6
strAstro[0] : c
strAstro[1] : o
strAstro[2] : s
strAstro[3] : m
strAstro[4] : i
strAstro[5] : c


But as we said, it will also append a null character. It will store the null character in the next consecutive byte, which may already contain data. Obviously this is not good.

Hope that helped ;)
arun1390! Nice work and well done example. I feel more at ease being part of a member here to ask questions that I don't understand. Thanks a lot.
Topic archived. No new replies allowed.