Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.8 kB
1
Indexable
Never
#include <string>
#include <iostream>
#include <vector>
using namespace std;

string words = "Я не понимаю, что со мной не так, со мной не так";
vector <string> words_arr;

string last_word(string words)
{
    int a = words.rfind(" "); 
    int b = words.rfind(", ") + 1;

    return words.substr(1 + max(a, b));
}

int word_end_index(string words, int start)
{
    int a = words.find(" ", start) - 1;
    int b = words.find(",", start) - 1;

    if (a < 0 && b < 0)
        return words.size() - 1;

    else if (a < 0)
        return b;

    else if (b < 0)
        return a;

    else
        return min(a, b);
}

void reverse_word(string& words, int word_start, int word_end)
{
    for (int i = 0; i < (word_end - word_start + 1) / 2; i++)
    {
        char c = words[word_start + i];
        words[word_start + i] = words[word_end - i];
        words[word_end - i] = c;
    }
}

void cpp()
{
    int last_word_len = last_word(words).size();

    for (int word_start = 0; word_start < words.size(); word_start++)
    {
        if (words[word_start] == ' ' || words[word_start] == ',')
            continue;
        int word_end = word_end_index(words, word_start);
        int word_len = word_end - word_start + 1;
        if (word_len > 3)
        {
            reverse_word(words, word_start, word_end);
        }
        else if (word_len < 3)
        {
            words_arr.push_back(words.substr(word_start, word_len));
        }
        // cout << words.substr(word_start, word_len) << endl;
        word_start = word_end;
    }

    for (string word : words_arr)
    {
        int start = words.find(word);
        words.erase(start, word.size());
    }

    cout << words;
}

int main()
{
    setlocale(LC_ALL, "Rus");
    cpp();
}