Compiler Version

You are converting an old code for a new version of the compiler.

In the old code, we have used "->" for pointers. But now we have to replace each "->" with a ".". But this replacement shouldn't be done inside comments. A comment is a string that starts with "//" and terminates at the end of the line.

Example:

Input: str = {"int t; //variable t", "t->a=0;  //t->a does something", "return 0;"}

Output:

int t; //variable t t.a=0; //t->a does something return 0;

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

void compilerVersion(vector<string&str)
{
    for (int j = 0j < str.size(); j++)
    {
        string s = str[j];
        int n = s.size();
        int i = 0;
        while (i < n)
        {
            if (i + 1 < n && s[i] == '/' && s[i + 1] == '/')
            {
                for (int j = ij < nj++)
                    cout << s[j];
                break;
            }
            else if (i + 1 < n && s[i] == '-' && s[i + 1] == '>')
            {
                cout << '.';
                i++;
            }
            else
                cout << s[i];
            i++;
        }
        cout << "\n";
    }
}
int main()
{

    vector<stringstr = {"int t; //variable t",
                          "t->a=0;  //t->a does something",
                          "return 0;"};

    compilerVersion(str);

    return 0;
}


No comments:

Post a Comment