Strings & arrays
Strings are sequences of characters used to represent textual data in programming.
An array is like a list of items, but each has a specific place. It's similar to lining up a bunch of boxes in a row. You can put things in these boxes (items) and easily find a specific box by knowing its position in the row (index).
Try to observe the similarity. An array is like a list of items but if I say that these items are characters then doesn't it seem similar to strings?
In C++, characters of a string behave similarly to elements of an array.
Now let's see how can we apply our array concepts to strings
Access String Characters
Just like array elements, we can access string characters by using indexes (similar to an array).
Suppose, we have a string like this:
string text = "Sea!";
Then, the indexing would be:
Example
#include <iostream>
using namespace std;
int main() {
string text = "Sea!";
// first character
cout << text[0] << endl; // S
// second character
cout << text[1] << endl; // e
// third character
cout << text[2] << endl; // a
//fourth character
cout << text[3]; // !
return 0;
}
Output
S
e
a
!
Problem
Write a C++ program to create a string 'HelloWorld' assigned to a variable named text
. Print the 8th element r
as the output.
Output
The 8th element of the string is: r
Solution
#include <iostream>
using namespace std;
int main() {
// Create a string 'HelloWorld' and assign it to the variable named text
string text = "HelloWorld";
// Print the 8th element of the string
cout << "The 8th element of the string is: " << text[7] << endl;
return 0;
}
Update String Characters
Remember how we updated array elements while studying arrays? In the same way, we can update string characters too. Let's see an example.
Example
#include <iostream>
using namespace std;
int main() {
string text = "Bolt";
cout << "Original text: " << text << endl;
// change the first character to 'G'
text[0] = 'G';
// change the fourth character to 'f'
text[3] = 'f';
cout << "Updated text: " << text;
return 0;
}
Output
Original text: Bolt
Updated text: Golf
Quiz
What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
string text = "Green";
text[0] = 'L';
text[3] = 'h';
text[1] = 'i';
cout << text;
return 0;
}
Iterating through a string
Does it need an introduction now XD? Your smart minds must have already figured this out. If not, then just recall how we used to iterate over arrays. We used loops! Similarly, we are going to use loops to iterate through a string too.
Example
#include <iostream>
using namespace std;
int main() {
string text = "Hello Coders :)";
// Note the size of the string 'text' and run a loop from 1 to size
for (int i = 0; i < 15; ++i) {
cout << text[i] << endl;
}
return 0;
}
Output
H
e
l
l
o
C
o
d
e
r
s
:
)
Iterating using a ranged for loop
Just like arrays, we can also iterate using a ranged for loop without worrying about the range to pass in a simple for loop.
Example
#include <iostream>
using namespace std;
int main() {
string text = "Hello Coders :)";
// Note the size of the string 'text' and run a loop from 1 to size
for (char var : text) {
cout << var << endl;
}
return 0;
}
Output
H
e
l
l
o
C
o
d
e
r
s
:
)