forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-strings.cpp
59 lines (43 loc) · 1.73 KB
/
08-strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
int main()
{
//A string of characters can be represented with the std::string type.
//Task 0: Create two strings s1 and s2, each with some initial text.
std::string s1{"hello"};
std::string s2{"world"};
//Task 1: Print out the value of s1
std::cout << "the string s1 is [" << s1 << "]" << std::endl;
std::cout << "the string s2 is [" << s2 << "]" << std::endl;
//Task 2: Create an empty string s3.
// Is an initializer necessary?
std::string s3{};
std::cout << "the string s3 is [" << s3 << "]" << std::endl;
//Task 3: Set s3 to contain the concatenation of s1 and s2,
// then print out the value of s3.
s3 = s1 + s2;
std::cout << "the string s1 is [" << s1 << "]" << std::endl;
std::cout << "the string s2 is [" << s2 << "]" << std::endl;
std::cout << "the string s3 is [" << s3 << "]" << std::endl;
//Task 4: Print out the length of s1, s2 and s3.
std::cout << "the length of the string s1 is " << s1.length() << std::endl;
std::cout << "the length of the string s2 is " << s2.size() << std::endl;
std::cout << "the length of the string s3 is " << s3.length() << std::endl;
std::cout << "the char at index 1 of s1 is " << s1.at(1) << std::endl;
s3.at(1) = 'X';
s3.push_back('!');
s3.push_back('!');
s3.push_back('!');
//Task 5: Iterate over each character in s3 (with two different kinds of
// loop) and print each character by itself.
for (char c : s3)
{
std::cout << c << std::endl;
}
std::cout << "-------------------------" << std::endl;
for (size_t i{0}; i < s3.length(); ++i)
{
std::cout << s3.at(i) << std::endl;
}
return 0;
}