C Search and Replace Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Warren Moxley

    C Search and Replace Function

    Hi there, i've been searching for a C String search and replace
    function. I need to find all occurrences of " " in a char* array, and
    replace them with another char, I know how to do this in managed
    environments, but i'm still learning C, does anyone have a quicky
    function handy? or point me in the right direction on how to
    acccomplish this? your help is greatly appreciated. Also a note, this
    is running on a embedded device, so i can't use external libraries, i
    will need to build, or find a function that i can put into my existing
    code.

    Thanks
    Warren
  • Robert Gamble

    #2
    Re: C Search and Replace Function

    On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
    Hi there, i've been searching for a C String search and replace
    function. I need to find all occurrences of " " in a char* array, and
    replace them with another char, I know how to do this in managed
    environments, but i'm still learning C, does anyone have a quicky
    function handy? or point me in the right direction on how to
    acccomplish this? your help is greatly appreciated. Also a note, this
    is running on a embedded device, so i can't use external libraries, i
    will need to build, or find a function that i can put into my existing
    code.
    If you just want to replace all instances of a single character with a
    different character as you indicate, all you need to do is scan each
    character of the string, check the value of the character and replace
    it with the new character if necessary. The process is very straight-
    forward, below is a sample function that does this:

    void replace_char (char *s, char find, char replace) {
    while (*s != 0) {
    if (*s == find)
    *s = replace;
    s++;
    }
    }

    If you need to replace substrings of more than one character the logic
    is a bit more complicated. If you need to perform replacement that
    can cause the resulting string to be larger than the original string
    you will also need a separate buffer or the ability to resize the
    original string.

    Robert Gamble

    Comment

    • Warren Moxley

      #3
      Re: C Search and Replace Function

      On Sat, 11 Aug 2007 17:16:25 -0000, Robert Gamble
      <rgamble99@gmai l.comwrote:
      >On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
      >Hi there, i've been searching for a C String search and replace
      >function. I need to find all occurrences of " " in a char* array, and
      >replace them with another char, I know how to do this in managed
      >environments , but i'm still learning C, does anyone have a quicky
      >function handy? or point me in the right direction on how to
      >acccomplish this? your help is greatly appreciated. Also a note, this
      >is running on a embedded device, so i can't use external libraries, i
      >will need to build, or find a function that i can put into my existing
      >code.
      >
      >If you just want to replace all instances of a single character with a
      >different character as you indicate, all you need to do is scan each
      >character of the string, check the value of the character and replace
      >it with the new character if necessary. The process is very straight-
      >forward, below is a sample function that does this:
      >
      >void replace_char (char *s, char find, char replace) {
      while (*s != 0) {
      if (*s == find)
      *s = replace;
      s++;
      }
      >}
      >
      >If you need to replace substrings of more than one character the logic
      >is a bit more complicated. If you need to perform replacement that
      >can cause the resulting string to be larger than the original string
      >you will also need a separate buffer or the ability to resize the
      >original string.
      >
      >Robert Gamble
      Awesome thanks so much for your help, still trying to wrap my head
      around c and strings and pointers etc, i'll get it eventually just
      might take me a bit. I was getting confused thinking i had to do more
      than that, still tying to grasp how c handles char arrays etc.

      Cheers !!
      Warren

      Comment

      • Robert Gamble

        #4
        Re: C Search and Replace Function

        On Aug 11, 1:38 pm, Warren Moxley <war...@easy-ebiz.comwrote:
        On Sat, 11 Aug 2007 17:16:25 -0000, Robert Gamble
        >
        >
        >
        <rgambl...@gmai l.comwrote:
        On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
        Hi there, i've been searching for a C String search and replace
        function. I need to find all occurrences of " " in a char* array, and
        replace them with another char, I know how to do this in managed
        environments, but i'm still learning C, does anyone have a quicky
        function handy? or point me in the right direction on how to
        acccomplish this? your help is greatly appreciated. Also a note, this
        is running on a embedded device, so i can't use external libraries, i
        will need to build, or find a function that i can put into my existing
        code.
        >
        If you just want to replace all instances of a single character with a
        different character as you indicate, all you need to do is scan each
        character of the string, check the value of the character and replace
        it with the new character if necessary. The process is very straight-
        forward, below is a sample function that does this:
        >
        void replace_char (char *s, char find, char replace) {
        while (*s != 0) {
        if (*s == find)
        *s = replace;
        s++;
        }
        }
        >
        If you need to replace substrings of more than one character the logic
        is a bit more complicated. If you need to perform replacement that
        can cause the resulting string to be larger than the original string
        you will also need a separate buffer or the ability to resize the
        original string.
        >
        Robert Gamble
        >
        Awesome thanks so much for your help, still trying to wrap my head
        around c and strings and pointers etc, i'll get it eventually just
        might take me a bit. I was getting confused thinking i had to do more
        than that, still tying to grasp how c handles char arrays etc.
        I would suggest picking up a good C book ("The C Programming Language
        2nd Edition" by K&R is quite good if you aren't new to programming)
        and reading the C FAQ <http://www.c-faq.com/>; sections 4, 6, and 8
        should help you get a better handle on strings and pointers.

        Robert Gamble

        Comment

        • Warren Moxley

          #5
          Re: C Search and Replace Function

          On Sat, 11 Aug 2007 18:04:08 -0000, Robert Gamble
          <rgamble99@gmai l.comwrote:
          >On Aug 11, 1:38 pm, Warren Moxley <war...@easy-ebiz.comwrote:
          >On Sat, 11 Aug 2007 17:16:25 -0000, Robert Gamble
          >>
          >>
          >>
          ><rgambl...@gma il.comwrote:
          >On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
          >Hi there, i've been searching for a C String search and replace
          >function. I need to find all occurrences of " " in a char* array, and
          >replace them with another char, I know how to do this in managed
          >environments , but i'm still learning C, does anyone have a quicky
          >function handy? or point me in the right direction on how to
          >acccomplish this? your help is greatly appreciated. Also a note, this
          >is running on a embedded device, so i can't use external libraries, i
          >will need to build, or find a function that i can put into my existing
          >code.
          >>
          >If you just want to replace all instances of a single character with a
          >different character as you indicate, all you need to do is scan each
          >character of the string, check the value of the character and replace
          >it with the new character if necessary. The process is very straight-
          >forward, below is a sample function that does this:
          >>
          >void replace_char (char *s, char find, char replace) {
          while (*s != 0) {
          if (*s == find)
          *s = replace;
          s++;
          }
          >}
          >>
          >If you need to replace substrings of more than one character the logic
          >is a bit more complicated. If you need to perform replacement that
          >can cause the resulting string to be larger than the original string
          >you will also need a separate buffer or the ability to resize the
          >original string.
          >>
          >Robert Gamble
          >>
          >Awesome thanks so much for your help, still trying to wrap my head
          >around c and strings and pointers etc, i'll get it eventually just
          >might take me a bit. I was getting confused thinking i had to do more
          >than that, still tying to grasp how c handles char arrays etc.
          >
          >I would suggest picking up a good C book ("The C Programming Language
          >2nd Edition" by K&R is quite good if you aren't new to programming)
          >and reading the C FAQ <http://www.c-faq.com/>; sections 4, 6, and 8
          >should help you get a better handle on strings and pointers.
          >
          >Robert Gamble
          Thank you for your help robert, no im not new to programming, just
          with C, mainly a .net, coldfusion developer, also ASM just never had
          the need to learn C til now hehe.

          Warren Moxley

          Comment

          • CBFalconer

            #6
            Re: C Search and Replace Function

            Warren Moxley wrote:
            >
            Hi there, i've been searching for a C String search and replace
            function. I need to find all occurrences of " " in a char* array,
            and replace them with another char, I know how to do this in
            managed environments, but i'm still learning C, does anyone have
            a quicky function handy? or point me in the right direction on
            how to acccomplish this? your help is greatly appreciated. Also
            a note, this is running on a embedded device, so i can't use
            external libraries, i will need to build, or find a function that
            i can put into my existing code.
            Take a look at id2id-20, available at:

            <http://cbfalconer.home .att.net/download/>

            under GPL, and written in portable standard C. Its code is quite
            compact, and suited to embedding. The library addition by the
            system is not compact, but allows for lots of things not of
            interest to you. Id2id operates on identifiers, not characters.

            --
            Chuck F (cbfalconer at maineline dot net)
            Available for consulting/temporary embedded and systems.
            <http://cbfalconer.home .att.net>


            --
            Posted via a free Usenet account from http://www.teranews.com

            Comment

            • Keith Thompson

              #7
              Re: C Search and Replace Function

              Robert Gamble <rgamble99@gmai l.comwrites:
              [...]
              void replace_char (char *s, char find, char replace) {
              while (*s != 0) {
              if (*s == find)
              *s = replace;
              s++;
              }
              }
              A minor style point: I would have written

              while (*s != '\0')

              to emphasize that we're looking for a null character, not just any old
              zero value. It's exactly equivalent, but I find '\0' clearer.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Mike Wahler

                #8
                Re: C Search and Replace Function


                "Keith Thompson" <kst-u@mib.orgwrote in message
                news:lnfy2p90rt .fsf@nuthaus.mi b.org...
                Robert Gamble <rgamble99@gmai l.comwrites:
                [...]
                >void replace_char (char *s, char find, char replace) {
                > while (*s != 0) {
                > if (*s == find)
                > *s = replace;
                > s++;
                > }
                >}
                >
                A minor style point: I would have written
                >
                while (*s != '\0')
                >
                to emphasize that we're looking for a null character, not just any old
                zero value. It's exactly equivalent, but I find '\0' clearer.
                I guess everyone has their own favorite style.
                I'd have used:

                while(*s)

                Less typing. :-)

                -Mike



                Comment

                • Richard

                  #9
                  Re: C Search and Replace Function

                  Warren Moxley <warren@easy-ebiz.comwrites:
                  Hi there, i've been searching for a C String search and replace
                  function. I need to find all occurrences of " " in a char* array, and
                  replace them with another char, I know how to do this in managed
                  environments, but i'm still learning C, does anyone have a quicky
                  function handy? or point me in the right direction on how to
                  acccomplish this? your help is greatly appreciated. Also a note, this
                  is running on a embedded device, so i can't use external libraries, i
                  will need to build, or find a function that i can put into my existing
                  code.
                  >
                  Thanks
                  Warren
                  for(;*s;*s++)
                  if(*s==oldchar)
                  *s=newchar;

                  or maybe as a one liner (just for a laugh and untested ....)

                  while(*s==oldch ar?*s++=newchar :*s++);

                  Comment

                  Working...