Advertisement
hurmawe

lab6

May 12th, 2022
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     SetConsoleCP(1251);
  10.     SetConsoleOutputCP(1251);
  11.  
  12.     cout << "Lab6" << endl;
  13.  
  14.  
  15.     //Задание 1//
  16.     /*cout << "Задание 1" << endl;
  17.     string str;
  18.     int count = 0;
  19.  
  20.     cout << "Введите предложение:\n";
  21.     getline(cin, str);
  22.  
  23.     for (int i = 1; i <= str.length(); i++)
  24.         if (str[i - 1] == 'а' && (str[i] == ' ' or str[i] == '\0'))
  25.             count++;
  26.  
  27.     if (count > 0)
  28.         cout << "Найдено слов: " << count << endl;
  29.     else
  30.         cout << "Нет таких слов" << endl;*/
  31.  
  32.         //Задание 2
  33.     //  cout << "Задание 2" << endl;
  34.     //  string str;
  35.     //
  36.     //  cout << "Введите предложение:\n";
  37.     //  getline(cin, str);
  38.     // 
  39.     //  for (int i = 0; i < str.length(); i++)
  40.     //      if (str[i] == ' ')
  41.     //      {
  42.     //          if (i > 0)
  43.     //              if (str[i - 1] != ' ')
  44.     //                  cout << endl;
  45.     //      }
  46.     //      else
  47.     //                  cout << str[i];
  48.  
  49.         //Задание 3//
  50.         //cout << "Задание 3" << endl;
  51.         //string str;
  52.         //string str_add = ".exe";
  53.  
  54.         //cout << "Введите имя файла:\n";
  55.         //getline(cin, str);
  56.  
  57.         //for (int i = str.length(); i > 0; i--)
  58.         //{
  59.         //  if (str[i] == ' ' and str[i-1] != ' ')
  60.         //      str.erase(i);
  61.         //  if (str[i] == '\\')
  62.         //      break;
  63.         //  if (str[i] == '.')
  64.         //      str.erase(i);
  65.         //}
  66.         //str.append(str_add);
  67.         //cout << str;
  68.  
  69.         //Задание 4//
  70.         /*cout << "Задание 4" << endl;
  71.         string str;
  72.         string name1;
  73.         string name2;
  74.         int number=0;
  75.  
  76.         cout << "Введите ФИО:\n";
  77.         getline(cin, str);
  78.  
  79.         for (int i = 0; i < str.length(); i++)
  80.         {
  81.             if (str[i] == ' ')
  82.             {
  83.                 if (i > 0)
  84.                     if (str[i - 1] != ' ')
  85.                     {
  86.                         number++;
  87.                         if (number == 2)
  88.                             break;
  89.                     }
  90.             }
  91.             else if (number)
  92.                 name1.push_back(str[i]);
  93.                  else
  94.                 name2.push_back(str[i]);
  95.         }
  96.         cout << "Результат\n" << "Привет, " << name1 << " " << name2 << endl;*/
  97.  
  98.         ////Задание 5//
  99.         //cout << "Задание 5" << endl;
  100.         //string str;
  101.         //
  102.         //string str_rep = "Юра";
  103.         //string str_old = "Вася";
  104.  
  105.         //cout << "Введите предложение :\n";
  106.         //getline(cin, str);
  107.         //int pos = 0;
  108.         //
  109.         //while(-1!=(pos=str.find(str_old,pos)))
  110.         //  str.replace(pos, str_old.length(), str_rep);
  111.         //cout << str;
  112.  
  113.     //Задание 6//
  114.     cout << "Задание 6" << endl;
  115.     string str;
  116.  
  117.     string str_rep = "Привет";
  118.     string str_old;
  119.  
  120.     cout << "Введите предложение :\n";
  121.     getline(cin, str);
  122.  
  123.     int pos=0;
  124.     int sign = 0;
  125.     for (int i = 0; i <= str.length(); i++)
  126.     {
  127.         if ((str[i] == '.') or (str[i] == ',') or (str[i] == '!') or (str[i] == '?'))
  128.         {
  129.             sign++;
  130.             continue;
  131.         }
  132.         if (str[i] == ' ' or str[i] == '\0')
  133.         {
  134.             if (i > 0)
  135.                 if (str[i - 1] != ' ')
  136.                 {
  137.                     pos = str.find(str_old, pos);
  138.                     str.replace(pos, str_old.length(), str_rep);
  139.                     i = pos + str_rep.length()+sign;
  140.                     str_old = "";
  141.                     sign = 0;
  142.                 }
  143.         }
  144.         else
  145.             str_old.push_back(str[i]);
  146.     }
  147.     cout << str;
  148.  
  149.  
  150.         return 0;
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement