Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <locale.h>
  4. #include <Windows.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9.  
  10. using namespace std;
  11.  
  12. bool isSameLetter(string word) {
  13.     if (word[0] == word[word.length() - 1]) return true;
  14.     else return false;
  15. }
  16.  
  17. int main()
  18. {
  19.     setlocale(LC_ALL, "rus");
  20.     SetConsoleCP(1251);
  21.     SetConsoleOutputCP(1251);
  22.  
  23.     vector <string> words;
  24.     string str;
  25.     cout << "Введите строку:" << endl;
  26.     getline(cin, str);
  27.    
  28.     string previousChar = " ";
  29.     string word = "";
  30.     //Если длина строки = 0, то вывести сообщение. Иначе:
  31.     //Если предыдущий символ - пробел, а порядковый - буква, то начать записывать слово.
  32.     //Если предыдущий символ - буква, а порядковый - пробел, точка или длина подошла к концу, то добавить слово в список.
  33.     if (str.length() == 0) cout << "Пустая строка." << endl;
  34.     else {
  35.         for (int i = 0; i < str.length(); i++) {
  36.             if ((i == 0 || previousChar == " " || isalpha(previousChar[0])) && isalpha(str[i])) {
  37.                 word += str[i];
  38.                 previousChar = str[i];
  39.             }
  40.             else if (isalpha(previousChar[0]) && (!isalpha(str[i]))) {
  41.                 previousChar = str[i];
  42.                 words.push_back(word);
  43.                 word = "";
  44.             }
  45.             if ((i == str.length() - 1) && (word!="")) {
  46.                 words.push_back(word);
  47.             }
  48.         }
  49.     }
  50.     for (int i = 0; i < words.size(); i++) {
  51.         cout << words[i] << endl;
  52.     }
  53.  
  54.     if (words.size() == 0) cout << "Строка из пробелов." << endl;
  55.     else {
  56.         int kolvo1 = 0;
  57.         for (int i = 0; i < words.size(); i++) {
  58.             if (isSameLetter(words[i])) kolvo1 += 1;
  59.         }
  60.         cout << "Количество слов начинающихся и заканчивающихся на одинаковую букву: " << kolvo1 << endl;
  61.         cout << endl;
  62.        
  63.         cout << "Задание 2: Удалить слова с заданной длиной. Введите длину: ";
  64.         int len2;
  65.         cin >> len2;
  66.         int pos;
  67.         /*
  68.         //сработает только если нет слов, содержащих в себе другие слова заданной длины как подстроку.
  69.         for (int i = 0; i < words.size(); i++) {
  70.             if (words[i].length() == len2) {
  71.                 pos = str.find(words[i]);
  72.                 str.erase(pos, pos + len2);
  73.             }
  74.         }
  75.         */
  76.         string strtemp = "";
  77.         for (int i = 0; i < words.size(); i++) {
  78.             if (words[i].length() != len2) {
  79.                 strtemp += (words[i] + " ");
  80.             }
  81.         }
  82.         //удалить последний пробел
  83.         //если нужно преобразовать изначальную строку, то:
  84.         //str = strtemp;
  85.         cout << strtemp << endl << endl;
  86.  
  87.         cout << "Массив из слов, в которых ни одна буква не повторяется:" << endl;
  88.         vector <string> uniqueWords;
  89.         vector <int> indexesOfNotUniqueWords;
  90.         bool unique = true;
  91.         for (int i = 0; i < words.size(); i++) {
  92.             for (int j = 0; j < words[i].length()-1; j++) {
  93.                 for (int k = j+1; k < words[i].length(); k++) {
  94.                     if (words[i][j] == words[i][k]) {
  95.                         unique = false;
  96.                         indexesOfNotUniqueWords.push_back(i);
  97.                         break;
  98.                     }
  99.                 }
  100.                 if (unique == false) {
  101.                     break;
  102.                 }
  103.             }
  104.             if (unique == true) {
  105.                 uniqueWords.push_back(words[i]);
  106.                 cout << words[i] << " ";
  107.             }
  108.             else unique = true;
  109.         }
  110.         cout << endl;
  111.         cout << "Массив индексов слов с повторяющимися буквами:" << endl;
  112.         for (int i = 0; i < indexesOfNotUniqueWords.size(); i++) {
  113.             cout << indexesOfNotUniqueWords[i] << " ";
  114.         }
  115.         cout << endl << endl;
  116.     }
  117.  
  118.  
  119.     system("pause");
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement