Нужно продублировать каждое третье слово в введённой строке С++

wstring s, a, b;
getline(ws(wcin), s);
int k = 0;
int slovo, count = 0;

int i = 0;
while (s[i] == ' ' && s[i] != '\0')
    i++;
slovo = 0;
while (s[i] != '\0') {
    if (s[i] != ' ' && slovo == 0)
    {
        slovo = 1;
        count++;
        if (count%3==0) 
        {
           
            int pos = i + 1;
            while (iswspace(s[pos])) pos--;
            while (!iswspace(s[pos])) pos--;

            int len = abs(pos - i);
            if (s[i + 1] == '\0') len++;

            while (--len >= 0)
            {
                for (int q = pos; s[q] != '\0'; q++)
                    a += s[q];
                b += L"" + a;
                s.insert(i - 1 , L"" + a);
                i += a.length() + 1;
                a = L"";
                count = 0;
            }
        }
    }
    else if (s[i] == ' ')
        slovo = 0;
    i++;
}
wcout << L"\nРезультат: ";

wcout << s;

Помогите пожалуйста доработать код, учитывая что строка может содержать любые символы кроме переноса строки, в том числе пробелы и символы табуляции.


Ответы (0 шт):