Advertisement
Viktor_Profa

Лабараторна работа №4

Apr 4th, 2024 (edited)
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. /*
  2. Завдання 1
  3. Відшукати слово «ріпка» у рядку та замінити його на слово «колобок». Вивести номер
  4. початкової позиції виконаної заміни. Якщо така заміна неможлива, то вивести про це
  5. інформацію.
  6. */
  7.  
  8. #include <iostream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.     string str = "У лісі жила ріпка, пухнаста й соковита.";
  15.     string wordToFind = "ріпка";
  16.     string wordToReplace = "колобок";
  17.  
  18.     size_t foundPos = str.find(wordToFind);
  19.    
  20.     if (foundPos != string::npos) {
  21.         str.replace(foundPos, wordToFind.length(), wordToReplace);
  22.         cout << "Слово \"" << wordToFind << "\" замінено на \"" << wordToReplace << "\". Початкова позиція заміни: " << foundPos << endl;
  23.     } else {
  24.         cout << "Слово \"" << wordToFind << "\" не знайдено у рядку." << endl;
  25.     }
  26.  
  27.     cout << "Результат: " << str << endl;
  28.  
  29.     return 0;
  30. }
  31. /*
  32. Завдання 2
  33. Дано масив слів, і в кожному слові від 2 до 10 малих латинських букв. У словах вилучити
  34. всі входження «th».
  35. */
  36.  
  37. #include <iostream>
  38. #include <vector>
  39. #include <string>
  40.  
  41. using namespace std;
  42.  
  43. int main() {
  44.     vector<string> words = {"hello", "there", "the", "weather", "is", "beautiful", "with", "thunderstorm"};
  45.  
  46.     for (string& word : words) {
  47.         size_t pos;
  48.         while ((pos = word.find("th")) != string::npos) {
  49.             word.erase(pos, 2); // Видаляємо "th"
  50.         }
  51.     }
  52.  
  53.     // Виведення результату
  54.     cout << "Масив слів після вилучення 'th': ";
  55.     for (const string& word : words) {
  56.         cout << word << " ";
  57.     }
  58.     cout << endl;
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement