lundi 31 août 2020

Replacing a substring with a space character

I am given a string and I have to remove a substring from it. Namely WUB, and replace it with a space character.

Input:  WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB
Output: WE ARE THE CHAMPIONS MY FRIEND 

Here is my code so far:

#include <iostream>

using namespace std;

int main()
{
    const string check = "WUB";
    string s, p;
    int ct = 0;
    cin >> s;

    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == 'W' && s[i+1] == 'U' && s[i+2] == 'B')
        {
            i += 2;
            if (p[ct] == '32' || p.empty())
            {
                continue;
            }
            else
            {
                p += ' ';
                ct++;
            }
        }
        else
        {
            p += s[i];
            ct++;
        }
    }

    cout << p;
    return 0;
}

Why is the first if statement never executed?

Aucun commentaire:

Enregistrer un commentaire