Advertisement
Petro_zzz

строки в стилях С и С++

May 15th, 2024
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void work_with_string() {
  7.  
  8.     cout << "Hello world!" << endl;
  9.     const char* str = "Hello world!";
  10.  
  11.     //char* str2 =  "Hi man";
  12.     char str2[]{"Hi man"}; // 'H', 'i', ' ', 'm', 'a', 'n', '\0'
  13.     cout << 'a' << "a" << endl;
  14.    
  15.     char* str3 = str2;
  16.     char str5[]{ 'H', 'i', ' ', 'B', 'o', 'b' };
  17.  
  18.     // терминальный ноль.
  19.     char str6[]{ 'H', 'i', ' ', 'B', 'o', 'b', '\0'};
  20.     cout << str2 << endl;
  21.     cout << str5 << endl;
  22.     cout << str6 << endl;
  23.  
  24.     // Работаем как с обычными массивами
  25.     cout << str[3] << " " << str[2] << endl;
  26.  
  27.     str2[3] = '@';
  28.     cout << str2 << endl;
  29.  
  30.  
  31.     //std::string
  32.     string str4 = "Hi girl";
  33.  
  34.  
  35.     // длинна строки
  36.     cout << str2 << endl;
  37.     cout << sizeof(str2) / sizeof(str2[0]) << endl;
  38.     cout << strlen(str2) << endl;
  39.    
  40.     const int size_str2 = sizeof(str2) / sizeof(str2[0]);
  41.     char deepcopy_str2[size_str2];
  42.     for (int k = 0; k < size_str2; k++)
  43.         deepcopy_str2[k] = str2[k];
  44.     deepcopy_str2[0] = '$';
  45.     cout << deepcopy_str2 << endl;
  46.     cout << str2 << endl;
  47.    
  48.     char copy_str2[1024];
  49.     strcpy_s(copy_str2, str2);
  50.  
  51.     copy_str2[0] = '&';
  52.     cout << copy_str2 << endl;
  53.     cout << str2 << endl;
  54.  
  55.     // compare - сравнение
  56.     char s1[1024] = "one ";
  57.     char s2[] = "two";
  58.  
  59.     cout << "COMPARE " << endl;
  60.     if (strcmp(s1, s2) < 0)
  61.         cout << s1 << " " << s2 << endl;
  62.     else
  63.         cout << s2 << " " << s1 << endl;
  64.    
  65.  
  66.  
  67.     // Конкатенация
  68.    
  69.    
  70.     cout << s1 << endl;
  71.     cout << s2 << endl;
  72.     strcat_s(s1, s2);
  73.     cout << "Concatenated: " << s1 << endl;
  74.  
  75.     // Поиск символа в строке
  76.     char* left_find = strchr(s1, 'o');
  77.     cout << left_find << endl;
  78.  
  79.     // Поиск символа в строке
  80.     char* right_find = strrchr(s1, 'o');
  81.     cout << right_find << endl;
  82.  
  83.     // Поиска подстроки в строке
  84.     char s3[]{"Hello world and good morning"};
  85.     char* s4 = strstr(s3, " and ");
  86.     cout << s4 + 5 << endl;
  87. }
  88.  
  89. void converter() {
  90.     double pi = 3.14;
  91.     int day = 7;
  92.     const char* s1 = "2.731";
  93.     const char* s2 = "12";
  94.  
  95.     int res1 = atoi(s2);
  96.     double res2 = atof(s1);
  97.  
  98.     cout << res1 << " " << res2 / 2.0 << endl;
  99.  
  100.     char res_s[1024];
  101.     _itoa_s(35, res_s, 36);
  102.     cout << res_s << endl; 
  103. }
  104.  
  105. void work_cpp_string() {
  106.     string str1 = "Hello world";
  107.     cout << str1 << endl;
  108.  
  109.     cout << str1[8] << endl;
  110.     str1[9] = '#';
  111.     cout << str1 << endl;
  112.  
  113.     // Конвертация к строкам в стиле C
  114.     const char* str2 = str1.data();
  115.     const char* str3 = str1.c_str();
  116.  
  117.     cout << str1.length() << endl;
  118.     cout << str1.size() << endl;
  119.  
  120.     string s1 = "one";
  121.     string s2 = "two";
  122.  
  123.     cout << s1 + " " + s2 << endl;
  124.  
  125.     string s3 = s2;
  126.  
  127.     s3[0] = '&';
  128.     cout << s3 << endl;
  129.     cout << s2 << endl;
  130.  
  131.     string s4 = to_string(3.12);
  132.     cout << s4 << endl;
  133.     cout << stod("4" + s4) << endl;
  134.  
  135.     cout << s1 << endl;
  136.     cout << s2 << endl;
  137.  
  138.     // compare - сравнение строк
  139.     if (s1 > s2)
  140.         cout << s1 << " " << s2 << endl;
  141.     else
  142.         cout << s2 << " " << s1 << endl;
  143.  
  144.     string text[]{ "one", "two", "three" };
  145.  
  146.  
  147. }
  148.  
  149. int main() {
  150.     work_with_string();
  151.     converter();
  152.     //work_cpp_string();
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement