Advertisement
Petro_zzz

new_lesson11_1

Sep 16th, 2022
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. void genCharArr(char* arr) {
  11.     //cout << sizeof(arr) / sizeof(arr[0]) << endl;
  12.     int sz = _msize(arr) / sizeof(arr[0]);
  13.     for (int k = 0; k < sz; ++k) {
  14.         arr[k] = rand() % 256;
  15.     }
  16. }
  17.  
  18. void showCharArr(char* arr) {
  19.     int sz = _msize(arr) / sizeof(arr[0]);
  20.     for (int k = 0; k < sz; ++k) {
  21.         cout << arr[k];
  22.     }
  23.     cout << endl;
  24. }
  25.  
  26. void task1() {
  27.     int sz = 10;
  28.     char *pArr = new char[sz];
  29.     //char Arr[13]{};
  30.     genCharArr(pArr);
  31.     showCharArr(pArr);
  32.     //cout << sizeof(Arr) / sizeof(Arr[0]) << endl;
  33.     delete[] pArr;
  34. }
  35.  
  36. void test_str2() {
  37.     char str1[100]{ "Hello" };
  38.     char str2[]{ " world" };
  39.     strcat_s(str1, str2);
  40.     strcat_s(str1, str2);
  41.     cout << str1 << endl;
  42.  
  43.     strncat_s(str1, str2, 20);
  44.     cout << str1 << endl;
  45.  
  46.     char* str3 = new char[]{ "hffhwadswsda" };
  47.     cout << strchr(str3, 'w') << endl;
  48.  
  49.     cout << "Compare: " << _stricmp("ABC", "aBC") << endl;
  50.     cout << "Compare: " << strcmp("DFG", "ABC") << endl;
  51.     cout << "Compare: " << strcmp("ABC", "ABC") << endl;
  52.  
  53.     cout << "Length: " << strlen("ABCDE fg") << endl;
  54.    
  55.     char strBig[]{ "ABCdefg" };
  56.     _strlwr_s(strBig);
  57.     cout << "small: " << strBig << endl;
  58.  
  59.     _strupr_s(strBig);
  60.     cout << "big: " << strBig << endl;
  61.  
  62.     _strrev(strBig);
  63.     cout << "reverce: " << strBig << endl;
  64.  
  65.     _strnset_s(strBig, '$', 12);
  66.     cout << strBig << endl;
  67.  
  68.     cout << strspn("ABCDEF", "EF") << endl; // ?????
  69.    
  70.     char strBig2[]{"Let me speak about the bus." };
  71.     char* first = strstr(strBig2, "buss");
  72.     if (first)
  73.         cout << "It find" << endl;
  74.     else
  75.         cout << "Dose not find." << endl;
  76.     //strtok_s(strBig2, " ", ppStr); // не знаем
  77.     double x = atof("12.5");
  78.     cout << x * 1.1 << endl;
  79.     int k = atoi("163");
  80.     cout << k + 23 << endl;
  81.     long long big = atoll("12345678998545");
  82.     cout << big << endl;
  83.     cout << "long long: " << sizeof(long long) << endl;
  84.     cout << "long: " << sizeof(long) << endl;
  85.     cout << "int: "  << sizeof(int) << endl;
  86.     if (!_itoa_s(1457.6565, strBig2, 10)) {
  87.         cout << strBig2 << endl;
  88.     }
  89. }
  90.  
  91. void test_str() {
  92.     /*
  93.     putchar('R');
  94.     char ch = getchar();
  95.     cout << "You enter char: " << ch << endl;
  96.    
  97.     ch = _getch(); // #include <conio.h>
  98.     cout << "You enter char: " << ch << endl;
  99.     */
  100.     char str[1024];
  101.     gets_s(str); //#include <stdio.h>
  102.     //cout << str;
  103.     puts("Hello world");
  104. }
  105.  
  106. void task2() {
  107.     auto name = "Hello world";
  108.     cout << name << endl;
  109.  
  110.     const char starr[] = "Hello world";
  111.     cout << starr << endl;
  112.  
  113.     const char* starr2 = new char[] {"Hello world"};
  114.     cout << starr2 << endl;
  115.  
  116.     char starr4[]{ "Hel\0 \t \n lo" };
  117.     char starr5[]{ 'H', 'e', 'l', 'l', 'o' };
  118.  
  119.     cout << sizeof(starr4) / sizeof(starr4[0]) << endl;
  120.     cout << sizeof(starr5) / sizeof(starr5[0]) << endl;
  121.     cout << starr5 << endl;
  122.  
  123.     cout << '\0' << endl;
  124.  
  125.     char ch1[]{"a"};
  126.     char ch2[]{'a'};
  127. }
  128.  
  129. void my_replace(
  130.     char* str,
  131.     char oldChar,
  132.     char newChar) {
  133.     int k = 0;
  134.     while (str[k]!= '\0') {
  135.         if (str[k] == oldChar)
  136.             str[k] = newChar;
  137.         ++k;
  138.     }  
  139. }
  140.  
  141. void my_replace(
  142.     char* &str,
  143.     char oldChar,
  144.     char* newChar) {
  145.     int size_str = _msize(str) / sizeof(str[0]);
  146.     int size_newChar = _msize(newChar) / sizeof(newChar[0]);
  147.    
  148.     int count = 0;
  149.     int k = 0;
  150.     while (str[k] != '\0') {
  151.         if (str[k] == oldChar)
  152.             count++;
  153.         ++k;
  154.     }
  155.     if (count > 0) {
  156.         char* buff = new char[size_str - count +  (size_newChar-1) * count];
  157.         int m = 0;
  158.         k = 0;
  159.         while (str[k] != '\0') {
  160.             if (str[k] == oldChar) {
  161.                 for (int n = 0; n < size_newChar - 1; ++n) {
  162.                     buff[m] = newChar[n];
  163.                     ++m;
  164.                 }
  165.             }
  166.             else {
  167.                 buff[m] = str[k];
  168.                 ++m;
  169.             }  
  170.             ++k;
  171.         }
  172.         buff[m] = str[k];
  173.         delete[] str;
  174.         str = buff;
  175.     }
  176. }
  177.  
  178.  
  179.  
  180. void test_my_replace() {
  181.     char* str = new char[] {"abcdeabcde"};
  182.     char* sub_str = new char[] {"xyz"};
  183.     my_replace(str, 'a', sub_str);
  184.     cout << str << endl;
  185.     delete[] str;
  186.     delete[] sub_str;
  187. }
  188.  
  189. void test_concat() {
  190.     char name[]{ "Ivan" };
  191.     char fathername[]{"Nikolaevic"};
  192.     char sername[]{"Kovalev"};
  193.  
  194.     char full_name[1024]{}; // "Ivan Nikolaevic Kovalev"
  195.  
  196.     strcat_s(full_name, name);
  197.     strcat_s(full_name, " ");
  198.     strcat_s(full_name, fathername);
  199.     strcat_s(full_name, " ");
  200.     strcat_s(full_name, sername);  
  201.  
  202.     cout << full_name << endl;
  203. }
  204.  
  205. void main() {
  206.     //task1();
  207.     //task2();
  208.     //test_str();
  209.     //task3();
  210.     //test_my_replace();
  211.  
  212.     test_concat();
  213.    
  214.  
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement