Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15561

Re: Search and replace c-style strings in c++

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Search and replace c-style strings in c++
Date 2012-04-25 12:08 +1200
Message-ID <9votkbFi68U5@mid.individual.net> (permalink)
References <5232b65f-cd4e-436e-aa07-d5e5696aac46@21g2000vbh.googlegroups.com> <9voqibFi68U4@mid.individual.net> <1d32ea92-5782-499a-8eb6-3f6de93927ed@m31g2000vbn.googlegroups.com>

Show all headers | View raw


On 04/25/12 11:38 AM, Mike Semko wrote:

Please don't snip attributions, it's rude.

 > I wrote:

>> Use std::string internally and copy the result to a C style string.
>
> Yes I thought about that option, but i am trying to practice using c-
> strings specifically.

Well unless you are learning C, you should take advantage of the 
improved tools C++ offers you.  Your code demonstrates how unpleasant 
string manipulation is in C!

> Assuming I can't use std::string is there any
> way to make this existing code more concise. Maybe I am overlooking
> something. The way I have to figure out the new length and dynamically
> create a string seems ugly at best.

You have to dynamically create the new string because your result has to 
be consistent, the caller has to know whether or not to free the result. 
  To get the size of the new string, you have to do what you have done.

You could save some time by stopping your replace loop once count 
instances have been replaced (especially if count==0!).  Extracting the 
two while loops into their own functions would make the code's logic 
clearer.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Search and replace c-style strings in c++ Mike Semko <msemko@gmail.com> - 2012-04-24 16:06 -0700
  Re: Search and replace c-style strings in c++ Ian Collins <ian-news@hotmail.com> - 2012-04-25 11:16 +1200
    Re: Search and replace c-style strings in c++ Mike Semko <msemko@gmail.com> - 2012-04-24 16:38 -0700
      Re: Search and replace c-style strings in c++ Ian Collins <ian-news@hotmail.com> - 2012-04-25 12:08 +1200
        Re: Search and replace c-style strings in c++ Mike Semko <msemko@gmail.com> - 2012-04-24 17:32 -0700
          Re: Search and replace c-style strings in c++ Ian Collins <ian-news@hotmail.com> - 2012-04-25 12:35 +1200
            Re: Search and replace c-style strings in c++ Mike Semko <msemko@gmail.com> - 2012-04-24 17:45 -0700
              Re: Search and replace c-style strings in c++ Old Wolf <oldwolf@inspire.net.nz> - 2012-05-06 22:50 -0700
      Re: Search and replace c-style strings in c++ Paavo Helde <myfirstname@osa.pri.ee> - 2012-04-24 22:58 -0500
  Re: Search and replace c-style strings in c++ scott@slp53.sl.home (Scott Lurndal) - 2012-04-25 00:00 +0000
    Re: Search and replace c-style strings in c++ Ian Collins <ian-news@hotmail.com> - 2012-04-25 12:11 +1200
      Re: Search and replace c-style strings in c++ scott@slp53.sl.home (Scott Lurndal) - 2012-04-25 14:51 +0000
        Re: Search and replace c-style strings in c++ Juha Nieminen <nospam@thanks.invalid> - 2012-04-26 07:52 +0000
          Re: Search and replace c-style strings in c++ yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-04-26 10:47 +0000
            Re: Search and replace c-style strings in c++ Juha Nieminen <nospam@thanks.invalid> - 2012-04-26 11:04 +0000
            Re: Search and replace c-style strings in c++ Tobias Müller <troplin@bluewin.ch> - 2012-04-26 18:43 +0000
              Re: Search and replace c-style strings in c++ Miles Bader <miles@gnu.org> - 2012-04-27 05:40 +0900
    Re: Search and replace c-style strings in c++ Miles Bader <miles@gnu.org> - 2012-04-25 10:28 +0900
  Re: Search and replace c-style strings in c++ Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-04-26 23:50 -0400
    Re: Search and replace c-style strings in c++ Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-04-28 09:46 -0400
  Re: Search and replace c-style strings in c++ "Miquel van Smoorenburg" <miquels@netscum.invalid> - 2012-05-06 20:03 +0000

csiph-web