Phlip said:
I kind'a meant "STL-obsessed". Not just templated.
Great; everyone posted a loop with a find!
My colleague came up with this. Somehow:
stringstream ss;
ostream_iterator<string> outIterator(ss);
transform( m_str.begin(), m_str.end(), outIterator,
swap_character(asciiChar, substituteString) );
m_str = ss.str();
Note that it's a member of some method that transforms a member string
m_str, and that it searches for a single character and replaces it with
a string.
Could one upgrade that to work on a string?
transform in this case works with single chars, you have to convert
to a sequence of strings in order to work with this algorithm.
My first example can be converted to work with transform
but that does not satisfies, if spaces are not regarded as delimiters.
I'm learning Haskell and functional programming,
so I found this as a good practicing
.
Here is another version of code in both Haskell and coresponding C++
which works as you expected, I hope.
note there is no single temporary string created in C++ code.
// begin C++ ---------------------
#include <iostream>
#include <utility>
using namespace std;
typedef pair<string::iterator,string::iterator> Tuple1;
typedef pair<Tuple1,string::iterator> Tuple2;
typedef pair<bool,Tuple2> Tuple3;
Tuple3
searchr_p(string::iterator srb,string::iterator sre,
string::iterator xsb,string::iterator xse,
string::iterator fndb,string::iterator fnde)
{
if(srb == sre && xsb == xse)
return Tuple3(true,Tuple2(Tuple1(fndb,fnde),xsb));
if(srb == sre)
return Tuple3(true,Tuple2(Tuple1(fndb,fnde),xsb));
if(xsb == xse)
return Tuple3(false,Tuple2(Tuple1(fndb,fnde),xsb));
++fnde;
if(*srb == *xsb)
return searchr_p(++srb,sre,++xsb,xse,fndb,fnde);
else
return Tuple3(false,Tuple2(Tuple1(fndb,fnde),++xsb));
}
string&
searchr(string& sr,
string& rp,
string::iterator xsb,string::iterator xse,
string& out
)
{
if(sr.empty() || rp.empty() || xsb == xse)return out;
Tuple3 fnd = searchr_p(sr.begin(),sr.end(),
xsb,xse,
xsb,xsb);
if(fnd.first)
{
out+= rp;
searchr(sr,rp,fnd.second.second,xse,out);
}
else
{
out.append(fnd.second.first.first,fnd.second.first.second);
searchr(sr,rp,fnd.second.second,xse,out);
}
return out;
}
int main()
{
string sr = "search",rp="replace",str=" able search baker search
charlie ";
string out;
cout<<searchr(sr,rp,str.begin(),str.end(),out)<<'\n';
}
// end C++
module Main where
import IO
main = do
sr <- getLine
rp <- getLine
str<- getLine
putStrLn $ "Working:" ++ sr ++ " " ++ rp ++ " " ++ str
putStrLn $ searchr sr rp str
putStrLn "Done"
{- search replace " able search baker search charlie " -}
-------------------------------------------------------------------------------
searchr :: String->String->String -> String
searchr [] _ xs = xs
searchr _ [] xs = xs
searchr _ _ [] = []
searchr sr rp xs | fst fnd = rp ++ searchr sr rp (snd $ snd fnd)
| otherwise = (fst $ snd fnd) ++
searchr sr rp (snd $ snd fnd)
where fnd = searchr' sr xs ""
searchr' :: String->String->String -> (Bool,(String,String))
searchr' [] [] fnd = (True,(fnd,[]))
searchr' [] xs fnd = (True,(fnd,xs))
searchr' sr [] fnd = (False,(fnd,[]))
searchr' (sr:srs) (x:xs) fndSoFar | sr == x = searchr' srs xs xxs
| otherwise = (False,(xxs,xs))
where xxs = x:fndSoFar