user1002563
user1002563

Reputation: 335

String class in C++

If I were to access the char * array inside a string, do the arrays include spaces?

Example.

String s = "1 3";

Would s[1] be " " or 3?

Thank you!

Upvotes: 2

Views: 319

Answers (2)

Coder12345
Coder12345

Reputation: 3753

Yes they do.

s[0] == '1';
s[1] == ' '; // equals 32 ascii
s[2] == '3';

It is zero-indexed array with starting element of 0 and ending element of n-1. Also note that s[1] is not " " (double quotes) but ' ' (single quotes) because single quotes indicate character literal.

Upvotes: 9

leo
leo

Reputation: 1117

Yes, it includes spaces. s[1] is ' '.

Spaces are characters as any other.

Upvotes: 13

Related Questions