Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <string> // library for string data type
- using namespace std;
- // necessary in order to work with any C++library
- // (in that case, with <string> and <iostream>)
- int main() {
- string s = "";
- /* creates empty string.
- One can initialize the string with any value.
- E.g.:
- string s = "abacaba";
- */
- for (char i = 'a'; i <= 'z'; i++)
- s += i;
- /* Adds symbols in alphabitical order.
- Attention!
- += works with complexity in proportion
- to the size of the part we add
- + (s = s + i) works in proportion
- to the total size of the string we obtain
- after addition
- So,
- for (int i = 0; i < int(1e5); i++)
- s = s + 'a';
- leads to TL,
- but
- for (int i = 0; i < int(1e5); i++)
- s += 'a';
- doesn't.
- */
- cout << s.size() << endl; // outputs string length
- cout << s.length() << endl; // the same as the previous one command
- int cnt_less_n = 0;
- for (int i = 0; i < s.size(); i++) {
- // shows how one can get i-th symbol of the string (0 <= i < s.size())
- if (s[i] < 'n') // comparison of two symbols (by their codes)
- cnt_less_n++;
- }
- cout << cnt_less_n << " symbols less than n in " << s << endl;
- // first parameter = index of the leading symbol in the substring
- // second parameter = length of substring
- cout << s.substr(3, 5) << endl;
- // if one need to get some suffix of the string,
- // he is permitted to omit the second parameter
- cout << s.substr(3) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement