I want to know how I can do this:

  1. Find "sqrt" in a string
  2. Replace "sqrt" with only "#"

I have the following code but its not working. Why?

size_t found = 0;
found = modifiedExpr.find("sqrt");
if(found!=std::string::npos)
{
    modifiedExpr.replace(modifiedExpr.find(sqrt),sqrt.length(),"#");
}

Dani AI

Generated

Short diagnosis: the original call failed because the identifier sqrt in the replace call was not a string literal (and can collide with the sqrt math function), and the code calls find repeatedly instead of reusing the found index. Declaring a proper search token (and not naming it sqrt) and handling multiple matches correctly fixes the problem.

A simple, robust loop (store the token, check npos, and advance the search position after each replace) keeps logic clear and avoids redundant calls:

#include <string>

std::string s = "sqrt(81) + 14 * sqrt(4)";
const std::string token = "sqrt";
size_t pos = 0;

while ((pos = s.find(token, pos)) != std::string::npos) {
    s.replace(pos, token.size(), "#");
    pos += 1; // advance past the replacement to avoid re-finding the same spot
}

If a single call is preferred, std::regex_replace performs a global replace in one step. To only match the function name (not parts of other identifiers) use word boundaries or a lookahead for the following (:

#include <regex>
#include <string>

std::string s = "sqrt(81) + 14 * sqrt(4)";
s = std::regex_replace(s, std::regex(R"(\bsqrt\b)"), "#");      // whole-word
// or
s = std::regex_replace(s, std::regex(R"(sqrt(?=\())"), "#");    // only when followed by '('

Notes tied to the thread: was correct to suggest using a string token; avoid naming that token sqrt. ’s single-replace approach is fine when there’s only one occurrence. ’s loop works; for large inputs prefer continuing the search from pos + 1 (or pos + replacement.size()) instead of restarting from the beginning. Always use token.size() rather than hard-coded lengths and test edge cases (overlapping patterns or empty replacements).

Recommended Answers

All 5 Replies

Whats is sqrt in modifiedExpr.replace(modifiedExpr.find(sqrt),sqrt.length(),"#");? Is it a string? You should be able to do:

string sqrt = "sqrt";
size_t pos = 0;
while((pos = modifiedExpr.find(sqrt, pos)) != std::string::npos)
{
    modifiedExpr.replace(pos, sqrt.size(), "#")
}

Hi joemeister.so, you know the string to be replaced no problem just follow this code you will get the output.

found = modifiedExpr.find("sqrt");
if (found != std::string::npos)
{
    modifiedExpr.replace(found, 4, "#");
}

it will work

Hi joemeister.so, you know the string to be replaced.

found = modifiedExpr.find("sqrt");
if (found != std::string::npos)
{
    modifiedExpr.replace(found, 4, "#");
}

no problem just follow this code you will get the output.

You never update the found variable so multiple instances will not be modified. Here is an example that does what I expect you want:

#include <iostream>
#include <string>

int main () {
    const std::string fname = "sqrt";
    const size_t len = fname.size ();

    std::string expr = "sqrt(81) + 14 * sqrt(4)";
    size_t idx = expr.find (fname);

    std::cout << "Original : " << expr << std::endl;

    while (idx != std::string::npos) {
        expr.replace (idx, len, "#");
        std::cout << "Updated  : " << expr << std::endl;
        idx = expr.find (fname);
    }

    std::cout << "Final    : " << expr << std::endl;

    return 0;
}

The output is:

Original : sqrt(81) + 14 * sqrt(4)
Updated  : #(81) + 14 * sqrt(4)
Updated  : #(81) + 14 * #(4)
Final    : #(81) + 14 * #(4)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.