All lessons

7. Strings

Indexing and building strings

0 of 5 activities0%

Reading 1

Characters inside a string

Open

s[i] is the character at index i. Indexes start at 0 and go to s.size() - 1.

You can change a character with s[i] = 'X'; Join strings with + or +=.

Looping from 0 to size()-1 visits every character.

string s = "cat";
cout << s[0] << endl;  // c
s[0] = 'b';
cout << s << endl;     // bat
cout << s + "s" << endl;

Check 2

Last character

Open

For string s = "code"; which expression is the last character?

Fill in 3

Append

Open

The operator often used to concatenate two strings is

Try it 4

Flip first letter

Open

Try other words.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Count vowels

Open

Read one word (lowercase letters). Print how many vowels it contains (a e i o u).

main.cpp
Loading editor…