Home > Enterprise >  c replace a string with another string without built in functions
c replace a string with another string without built in functions

Time:12-11

void substitute(string stringuser, string subs1, string subs2)
{
    string vchar;
    for (int i = 0; i < stringuser.length(); i  )
    {
        vchar = stringuser[i];
        if (vchar == subs1)
        {
            newstring =subs2;
        }
        else
        {
            vchar = stringuser[i];
            newstring =vchar;
        }
    }
}

this is the code I wrote to replace a character in a string with another character. However, it should work for 2 or more characters withal, not only 1. The problem is I have no idea how to do it and I'd appreciate any help I can get with the task :)

CodePudding user response:

std::string has find() and replace() methods that work with substrings, eg:

string substitute(string stringuser, string subs1, string subs2)
{
    string::size_type pos, start = 0;
    while ((pos = stringuser.find(subs1, start)) != string::npos)
    {
        stringuser.replace(pos, subs1.size(), subs2);
        start = pos   subs2.size();
    }
    return stringuser;
}

If you are not allowed to use them, then replace them with your own implementations, eg:

string::size_type indexOf(const string &stringuser, const string &subs, string::size_type start = 0)
{
    if (start < stringuser.size())
    {
        for(string::size_type i = start; i < stringuser.size();   i)
        {
            if (stringuser[i] == subs[0])
            {
                if ((stringuser.size() - i) < subs.size()) {
                    break;
                }
                bool match = true;
                for(string::size_type j = 1; j < subs.size();   j)
                {
                    if (stringuser[i j] != subs[j])
                    {
                        match = false;
                        break;
                    }
                }
                if (match) {
                    return i;
                }
            }
        }
    }
    return string::npos;
}

string subString(const string &stringuser, string::size_type pos = 0, string::size_type count = string::npos)
{
    if (pos > stringuser.size()) {
         throw std::out_of_range("");
    }
    if ((count == string::npos) || ((stringuser.size() - pos) > count) {
        count = stringuser.size() - pos;
    }
    string newstring;
    newstring.reserve(count);
    for(string::size_type i = 0; i < count;   i) {
        newstring  = stringuser[pos i];
    }
    return newstring;
}

string substitute(const string &stringuser, const string &subs1, const string &subs2)
{
    string newstring;
    string::size_type pos, start = 0;
    while ((pos = indexOf(stringuser, subs1, start)) != string::npos)
    {
        if (pos > start) {
            newstring  = subString(stringuser, start, pos - start);
        }
        newstring  = subs2;
        start = pos   subs2.size();
    }
    if (start < stringuser.size()) {
        newstring  = subString(stringuser, start);
    }
    return newstring;
}

CodePudding user response:

You are trying to compare one character with a whole string, but you have to also check characters next to stringuser[i]. It can be done this way:

string substitute(string stringuser, string subs1, string subs2)
{
    string newstring;
    char vchar;
    for (int i = 0; i < stringuser.length(); i  )
    {
        vchar = stringuser[i];
        if (vchar == subs1[0])
        {
            int j;
            for (j = 1; j < subs1.length(); j  )    //We have to compare all symbols
            {
                if (i   j >= stringuser.length())
                    break;
                if (subs1[j] != stringuser[i   j])
                    break;
            }
            if (j == subs1.length())    //If cycle ended succesfully
            {
                newstring  = subs2;
                i  = subs1.length()-1;  //Skip remaining characters from subs1  
            }
            else
                newstring  = vchar;
        }
        else
        {
            vchar = stringuser[i];
            newstring  = vchar;
        }
    }
    return newstring;
}
  • Related