top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Write a function to find a sub string in a given string and replace it with another string?

+1 vote
645 views
C: Write a function to find a sub string in a given string and replace it with another string?
posted Apr 2, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
An example would be helpful

1 Answer

0 votes

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}

answer Apr 5, 2016 by Shubham Singh
Similar Questions
+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

...