kanden
kanden

Reputation: 187

Replace single quote with two single quotes in a string using c++

The below function is working as expected. But I think I can do it in an efficient way.

input = "Hello' Main's World";

Function Return Value "Hello'' Main''s World";

string  ReplaceSingleQuote(string input)
{

int len = input.length();
int i = 0, j =0;
char str[255];
sprintf(str, input.c_str()); 
char strNew[255];

for (i = 0; i <= len; i++) 
{
    if (str[i] == '\'') 
    {
        strNew[j] = '\'';
        strNew[j+ 1] = '\'';
        j = j + 2;
    } else
    {
        strNew[j] = str[i];
        j = j + 1 ;
    }
}

return strNew;

}

Upvotes: 3

Views: 4799

Answers (5)

Jamie
Jamie

Reputation: 1923

James Kanze's answer is a fine one. Just for the heck of it, though, I'll offer one that's a little more C++11ish.

string DoubleQuotes(string value)
{
    string retval;
    for (auto ch : value)
    {
        if (ch == '\'')
        {
            retval.push_back('\'');
        }
        retval.push_back(ch);
    }
    return retval;
}

Upvotes: 2

James Kanze
James Kanze

Reputation: 153909

The obvious solution is:

std::string
replaceSingleQuote( std::string const& original )
{
    std::string results;
    for ( std::string::const_iterator current = original.begin();
            current != original.end();
            ++ current ) {
        if ( *current == '\'' ) {
            results.push_back( '\'');
        }
        results.push_back( *current );
    }
    return results;
}

Small variations might improve performance:

std::string
replaceSingleQuote( std::string const& original )
{
    std::string results(
        original.size() 
            + std::count( original.begin(), original.end(), '\''),
        '\'' );
    std::string::iterator dest = results.begin();
    for ( std::string::const_iterator current = original.begin();
            current != original.end();
            ++ current ) {
        if ( *current == '\'' ) {
            ++ dest;
        }
        *dest = *current;
        ++ dest;
    }
    return results;
}

might be worth trying, for example. But only if you find the original version to be a bottleneck in your code; there's no point in making something more complicated than necessary.

Upvotes: 1

hmjd
hmjd

Reputation: 121961

A possibility (that will modify input) is to use std::string::replace() and std::string::find():

size_t pos = 0;
while (std::string::npos != (pos = input.find("'", pos)))
{
    input.replace(pos, 1, "\'\'", 2);
    pos += 2;
}

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258568

Maybe use a std::stringstream:

string  ReplaceSingleQuote(string input)
{
    stringstream s;
    for (i = 0; i <= input.length(); i++) 
    {
        s << input[i];
        if ( input[i] == '\'' )
           s << '\'';
    }
    return s.str();
}

Upvotes: 1

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

boost::replace_all(str, "'", "''");

http://www.boost.org/doc/libs/1_49_0/doc/html/boost/algorithm/replace_all.html

Upvotes: 3

Related Questions