Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- void work_with_string() {
- cout << "Hello world!" << endl;
- const char* str = "Hello world!";
- //char* str2 = "Hi man";
- char str2[]{"Hi man"}; // 'H', 'i', ' ', 'm', 'a', 'n', '\0'
- cout << 'a' << "a" << endl;
- char* str3 = str2;
- char str5[]{ 'H', 'i', ' ', 'B', 'o', 'b' };
- // терминальный ноль.
- char str6[]{ 'H', 'i', ' ', 'B', 'o', 'b', '\0'};
- cout << str2 << endl;
- cout << str5 << endl;
- cout << str6 << endl;
- // Работаем как с обычными массивами
- cout << str[3] << " " << str[2] << endl;
- str2[3] = '@';
- cout << str2 << endl;
- //std::string
- string str4 = "Hi girl";
- // длинна строки
- cout << str2 << endl;
- cout << sizeof(str2) / sizeof(str2[0]) << endl;
- cout << strlen(str2) << endl;
- const int size_str2 = sizeof(str2) / sizeof(str2[0]);
- char deepcopy_str2[size_str2];
- for (int k = 0; k < size_str2; k++)
- deepcopy_str2[k] = str2[k];
- deepcopy_str2[0] = '$';
- cout << deepcopy_str2 << endl;
- cout << str2 << endl;
- char copy_str2[1024];
- strcpy_s(copy_str2, str2);
- copy_str2[0] = '&';
- cout << copy_str2 << endl;
- cout << str2 << endl;
- // compare - сравнение
- char s1[1024] = "one ";
- char s2[] = "two";
- cout << "COMPARE " << endl;
- if (strcmp(s1, s2) < 0)
- cout << s1 << " " << s2 << endl;
- else
- cout << s2 << " " << s1 << endl;
- // Конкатенация
- cout << s1 << endl;
- cout << s2 << endl;
- strcat_s(s1, s2);
- cout << "Concatenated: " << s1 << endl;
- // Поиск символа в строке
- char* left_find = strchr(s1, 'o');
- cout << left_find << endl;
- // Поиск символа в строке
- char* right_find = strrchr(s1, 'o');
- cout << right_find << endl;
- // Поиска подстроки в строке
- char s3[]{"Hello world and good morning"};
- char* s4 = strstr(s3, " and ");
- cout << s4 + 5 << endl;
- }
- void converter() {
- double pi = 3.14;
- int day = 7;
- const char* s1 = "2.731";
- const char* s2 = "12";
- int res1 = atoi(s2);
- double res2 = atof(s1);
- cout << res1 << " " << res2 / 2.0 << endl;
- char res_s[1024];
- _itoa_s(35, res_s, 36);
- cout << res_s << endl;
- }
- void work_cpp_string() {
- string str1 = "Hello world";
- cout << str1 << endl;
- cout << str1[8] << endl;
- str1[9] = '#';
- cout << str1 << endl;
- // Конвертация к строкам в стиле C
- const char* str2 = str1.data();
- const char* str3 = str1.c_str();
- cout << str1.length() << endl;
- cout << str1.size() << endl;
- string s1 = "one";
- string s2 = "two";
- cout << s1 + " " + s2 << endl;
- string s3 = s2;
- s3[0] = '&';
- cout << s3 << endl;
- cout << s2 << endl;
- string s4 = to_string(3.12);
- cout << s4 << endl;
- cout << stod("4" + s4) << endl;
- cout << s1 << endl;
- cout << s2 << endl;
- // compare - сравнение строк
- if (s1 > s2)
- cout << s1 << " " << s2 << endl;
- else
- cout << s2 << " " << s1 << endl;
- string text[]{ "one", "two", "three" };
- }
- int main() {
- work_with_string();
- converter();
- //work_cpp_string();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement