c++ - How can I replace percent sign (%) with two %'s? -


i want try replace percentage sign in char array 2 %% signs. because % sign causes problems, if write output char array. therefore percentage sign must replaced 2 %% signs without using string.

// array causes dump because of '%' char input[] = "this text % charakter"; //therefore percent sign(%) must replaced 2 %%.  

you can use std::string handle necessary memory re-allocations you, plus boost algorithm make easier:

#include <string> #include <iostream> #include <boost/algorithm/string.hpp>  int main() {   std::string input("this text % charakter , % charakter");   boost::replace_all(input, "%", "%%");   std::cout << input << std::endl; } 

output:

this text %% charakter , %% charakter

if can't use boost, can write own version of replace_all using std::string::find , std::string::replace:

template <typename c> void replace_all(std::basic_string<c>& in,                   const c* old_cstring,                   const c* new_cstring) {   std::basic_string<c> old_string(old_cstring);   std::basic_string<c> new_string(new_cstring);   typename std::basic_string<c>::size_type pos = 0;   while((pos = in.find(old_string, pos)) != std::basic_string<c>::npos)   {      in.replace(pos, old_string.size(), new_string);      pos += new_string.size();   } } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -