tags:

views:

114

answers:

3

I want to know from where the index of a string and an array starts from. I am getting a lot of confusion while making programs.

While calculating string length of a string is the null character also counted?

+2  A: 

The index starts from ZERO.

Kangkan
then i also want to know whether null character is also counted while calculating string length of a string.
anurag18294
If you wish to iterate through the string's character in the fashion of a char * in that case, the '\n' will also be there. The array of char will be of length equal to the length of the string PLUS one.
Kangkan
A: 

Not entirely clear what you're asking.

If you're asking whether strings and arrays start at index 0, then Yes.

http://en.wikipedia.org/wiki/Zeroth

Assaf Lavie
+3  A: 

In C, C++, Java, and Python, array indices are 0-based, so they range from 0 to length-1. Some mathematically-oriented programming languages such as Matlab are 1-based. As a general rule of thumb, "real programming languages" tend to use 0-based indexing; mathematical/protoyping/modeling languages or other domain-specific languages may use 0-based or 1-based indexing, with 1-based common for math.

In terms of strings, the length of a string usually refers to the number of characters in the string excluding the terminating NUL-character, while the length of the buffer refers to the entire buffer's length, including the terminating NUL. You will find that std::string::size() and strlen() return the number of characters in the string, excluding the terminating NUL (i.e. the string length). The length of the buffer is actually one more than that.

Michael Aaron Safyan