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


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

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

From scott@slp53.sl.home (Scott Lurndal)
Subject Re: Search and replace c-style strings in c++
Newsgroups comp.lang.c++
References <5232b65f-cd4e-436e-aa07-d5e5696aac46@21g2000vbh.googlegroups.com>
Organization
Organization UseNetServer - www.usenetserver.com
Message-ID <u8Hlr.140$Sl3.92@news.usenetserver.com> (permalink)
Date 2012-04-25 00:00 +0000

Show all headers | View raw


Mike Semko <msemko@gmail.com> writes:
>I am trying to write a function that takes three c-style strings, and
>returns a c-style string. This function searches a c-string for all
>occurrences of the sub-string and replaces them with a different
>string.
>This program works but seems very inelegant. I can't help the feeling
>like it could have been done in a less bulky way.
>
>
>#include <iostream>
>#include <cstring>
>
>using namespace std;
>
>int main()
>{
>
>char* replaceSubstring(char*,char*,char*);
>
>char *string1, *string2, *string3;
>string1 = "the dog jumped over the fence";
>string2 = "the";
>string3 = "that";
>cout << replaceSubstring(string1,string2,string3);
>
>}
>
>
>char* replaceSubstring(char *original, char *from, char *to)
>{
>     int origlen = strlen(original);
>     int i = 0;
>     int count = 0;
>     char *ptr;
>     while (i<origlen)
>     {
>           ptr = strstr(original+i, from);
>           if (!ptr)
>               break;
>           else
>           {
>               i = ptr - original + 1;
>               count++;
>           }
>     }
>     int newsize = origlen + (strlen(to) - strlen(from)) * count;
>     char *newstring = new char[newsize];

Memory leak.

Why not just scan the input byte by byte for the first character of the
match string, and when found do a strncmp(), if fails, search for the
next occurance of the first match string character, copying unmatched bytes to the destination.

if the input strings may be utf-8 instead of ISO-8859-1 or ASCII, then
you've more work.

Make sure that every "new" has a corresponding "delete" somewhere.

scott

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