Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <locale.h>
- #include <Windows.h>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cstring>
- using namespace std;
- bool isSameLetter(string word) {
- if (word[0] == word[word.length() - 1]) return true;
- else return false;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- vector <string> words;
- string str;
- cout << "Введите строку:" << endl;
- getline(cin, str);
- string previousChar = " ";
- string word = "";
- //Если длина строки = 0, то вывести сообщение. Иначе:
- //Если предыдущий символ - пробел, а порядковый - буква, то начать записывать слово.
- //Если предыдущий символ - буква, а порядковый - пробел, точка или длина подошла к концу, то добавить слово в список.
- if (str.length() == 0) cout << "Пустая строка." << endl;
- else {
- for (int i = 0; i < str.length(); i++) {
- if ((i == 0 || previousChar == " " || isalpha(previousChar[0])) && isalpha(str[i])) {
- word += str[i];
- previousChar = str[i];
- }
- else if (isalpha(previousChar[0]) && (!isalpha(str[i]))) {
- previousChar = str[i];
- words.push_back(word);
- word = "";
- }
- if ((i == str.length() - 1) && (word!="")) {
- words.push_back(word);
- }
- }
- }
- for (int i = 0; i < words.size(); i++) {
- cout << words[i] << endl;
- }
- if (words.size() == 0) cout << "Строка из пробелов." << endl;
- else {
- int kolvo1 = 0;
- for (int i = 0; i < words.size(); i++) {
- if (isSameLetter(words[i])) kolvo1 += 1;
- }
- cout << "Количество слов начинающихся и заканчивающихся на одинаковую букву: " << kolvo1 << endl;
- cout << endl;
- cout << "Задание 2: Удалить слова с заданной длиной. Введите длину: ";
- int len2;
- cin >> len2;
- int pos;
- /*
- //сработает только если нет слов, содержащих в себе другие слова заданной длины как подстроку.
- for (int i = 0; i < words.size(); i++) {
- if (words[i].length() == len2) {
- pos = str.find(words[i]);
- str.erase(pos, pos + len2);
- }
- }
- */
- string strtemp = "";
- for (int i = 0; i < words.size(); i++) {
- if (words[i].length() != len2) {
- strtemp += (words[i] + " ");
- }
- }
- //удалить последний пробел
- //если нужно преобразовать изначальную строку, то:
- //str = strtemp;
- cout << strtemp << endl << endl;
- cout << "Массив из слов, в которых ни одна буква не повторяется:" << endl;
- vector <string> uniqueWords;
- vector <int> indexesOfNotUniqueWords;
- bool unique = true;
- for (int i = 0; i < words.size(); i++) {
- for (int j = 0; j < words[i].length()-1; j++) {
- for (int k = j+1; k < words[i].length(); k++) {
- if (words[i][j] == words[i][k]) {
- unique = false;
- indexesOfNotUniqueWords.push_back(i);
- break;
- }
- }
- if (unique == false) {
- break;
- }
- }
- if (unique == true) {
- uniqueWords.push_back(words[i]);
- cout << words[i] << " ";
- }
- else unique = true;
- }
- cout << endl;
- cout << "Массив индексов слов с повторяющимися буквами:" << endl;
- for (int i = 0; i < indexesOfNotUniqueWords.size(); i++) {
- cout << indexesOfNotUniqueWords[i] << " ";
- }
- cout << endl << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement