My own version of replace

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

    #1

    My own version of replace

    Hi all!

    I try to code my own version of replace but it doesn't work. I hope
    somebody can help me out.
    It only replaces the first char!.

    Why I don't use the public string.replace function? Well I love
    implementing string functions :-).

    I coded another function for replacing chars in a word and this
    function works!

    Thank you for any help

    Use:

    string text = "Good Morning";
    replaceStr(text ,"Morning","Eve ning");
    Console.WriteLi ne(text);

    Output: Good Eorning // Only the first char

    Here is the function

    public static void replaceStr(stri ng word, string word2, string
    replace)
    {
    int i;
    int x = 0, y, z = 0, l = 0;
    char[] place = new char[word.Length];
    for (i = 0; i < word.Length; i++)
    {
    place[x] = word[i]; // fills the place Array
    x++;
    }

    for (y = 0; y < place.Length; y++)
    {

    if (place[y] == word2[z]) /
    {

    place[y] = replace[l]; // It only replaces the
    first char
    l++;
    y++;
    z++;

    }


    }

    Console.WriteLi ne(place);

    }
  • DeveloperX

    #2
    Re: My own version of replace

    On 29 Nov, 17:08, implement <tomico...@gmai l.comwrote:
    Hi all!
    >
    I try to code my own version of replace but it doesn't work. I hope
    somebody can help me out.
    It only replaces the first char!.
    >
    Why I don't use the public string.replace function? Well I love
    implementing string functions :-).
    >
    I coded another function for replacing chars in a word and this
    function works!
    >
    Thank you for any help
    >
    Use:
    >
    string text = "Good Morning";
    replaceStr(text ,"Morning","Eve ning");
    Console.WriteLi ne(text);
    >
    Output: Good Eorning // Only the first char
    >
    Here is the function
    >
    public static void replaceStr(stri ng word, string word2, string
    replace)
    {
    int i;
    int x = 0, y, z = 0, l = 0;
    char[] place = new char[word.Length];
    for (i = 0; i < word.Length; i++)
    {
    place[x] = word[i]; // fills the place Array
    x++;
    }
    >
    for (y = 0; y < place.Length; y++)
    {
    >
    if (place[y] == word2[z]) /
    {
    >
    place[y] = replace[l]; // It only replaces the
    first char
    l++;
    y++;
    z++;
    >
    }
    >
    }
    >
    Console.WriteLi ne(place);
    >
    }
    You've double incremented y. There's a second issue you'll find when
    you try the test line with

    replaceStr("GoM od Morning", "Morning", "Evening");

    Comment

    • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

      #3
      RE: My own version of replace



      "implement" wrote:
      Hi all!
      >
      I try to code my own version of replace but it doesn't work. I hope
      somebody can help me out.
      It only replaces the first char!.
      >
      Why I don't use the public string.replace function? Well I love
      implementing string functions :-).
      >
      I coded another function for replacing chars in a word and this
      function works!
      >
      Thank you for any help
      >
      Use:
      >
      string text = "Good Morning";
      replaceStr(text ,"Morning","Eve ning");
      Console.WriteLi ne(text);
      >
      Output: Good Eorning // Only the first char
      >
      Here is the function
      >
      public static void replaceStr(stri ng word, string word2, string
      replace)
      {
      int i;
      int x = 0, y, z = 0, l = 0;
      char[] place = new char[word.Length];
      for (i = 0; i < word.Length; i++)
      {
      place[x] = word[i]; // fills the place Array
      x++;
      }
      >
      for (y = 0; y < place.Length; y++)
      {
      >
      if (place[y] == word2[z]) /
      {
      >
      place[y] = replace[l]; // It only replaces the
      first char
      l++;
      y++;
      z++;
      >
      }
      >
      >
      }
      >
      Console.WriteLi ne(place);
      >
      }
      >
      I think you are going to have more problems than this, but, you should not
      be incrimenting y (y++) inside your if statement. You end up missing a
      character.

      After the change, try running your code on the following:

      string text = "Guten Morgen";
      replaceStr(text ,"Morning","Abe nd");
      Console.WriteLi ne(text);

      You start to replace the word Morgen before completely checking if it equals
      Morning.


      Comment

      • Brian Gideon

        #4
        Re: My own version of replace

        On Nov 29, 11:08 am, implement <tomico...@gmai l.comwrote:
        Hi all!
        >
        I try to code my own version of replace but it doesn't work. I hope
        somebody can help me out.
        It only replaces the first char!.
        >
        Why I don't use the public string.replace function? Well I love
        implementing string functions :-).
        >
        I coded another function for replacing chars in a word and this
        function works!
        >
        Thank you for any help
        >
        Use:
        >
        string text = "Good Morning";
        replaceStr(text ,"Morning","Eve ning");
        Console.WriteLi ne(text);
        >
        Output: Good Eorning // Only the first char
        >
        Here is the function
        >
        public static void replaceStr(stri ng word, string word2, string
        replace)
        {
        int i;
        int x = 0, y, z = 0, l = 0;
        char[] place = new char[word.Length];
        for (i = 0; i < word.Length; i++)
        {
        place[x] = word[i]; // fills the place Array
        x++;
        }
        >
        for (y = 0; y < place.Length; y++)
        {
        >
        if (place[y] == word2[z]) /
        {
        >
        place[y] = replace[l]; // It only replaces the
        first char
        l++;
        y++;
        z++;
        >
        }
        >
        }
        >
        Console.WriteLi ne(place);
        >
        }
        That's because y is incremented twice on each interation of the loop.

        Even after you fix that there's still some problems. First, it will
        replace things it should not be replacing. Second, strings are
        immutable so unless you return a new string that function is basically
        nothing more than a CPU consuming noop.

        Comment

        • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

          #5
          RE: My own version of replace

          Maybe (as others have alluded) instead of reinventing the wheel, you could
          look into the REGEX replace method that will allow you to do lots of this
          kind of stuff without the tribulations of time - consuming custom methods?

          --Peter
          "Inside every large program, there is a small program trying to get out."






          "implement" wrote:
          Hi all!
          >
          I try to code my own version of replace but it doesn't work. I hope
          somebody can help me out.
          It only replaces the first char!.
          >
          Why I don't use the public string.replace function? Well I love
          implementing string functions :-).
          >
          I coded another function for replacing chars in a word and this
          function works!
          >
          Thank you for any help
          >
          Use:
          >
          string text = "Good Morning";
          replaceStr(text ,"Morning","Eve ning");
          Console.WriteLi ne(text);
          >
          Output: Good Eorning // Only the first char
          >
          Here is the function
          >
          public static void replaceStr(stri ng word, string word2, string
          replace)
          {
          int i;
          int x = 0, y, z = 0, l = 0;
          char[] place = new char[word.Length];
          for (i = 0; i < word.Length; i++)
          {
          place[x] = word[i]; // fills the place Array
          x++;
          }
          >
          for (y = 0; y < place.Length; y++)
          {
          >
          if (place[y] == word2[z]) /
          {
          >
          place[y] = replace[l]; // It only replaces the
          first char
          l++;
          y++;
          z++;
          >
          }
          >
          >
          }
          >
          Console.WriteLi ne(place);
          >
          }
          >

          Comment

          • implement

            #6
            Re: My own version of replace

            On 29 Nov., 18:33, Brian Gideon <briangid...@ya hoo.comwrote:
            On Nov 29, 11:08 am, implement <tomico...@gmai l.comwrote:
            >
            >
            >
            >
            >
            Hi all!
            >
            I try to code my own version of replace but it doesn't work. I hope
            somebody can help me out.
            It only replaces the first char!.
            >
            Why I don't use the public string.replace function? Well I love
            implementing string functions :-).
            >
            I coded another function for replacing chars in a word and this
            function works!
            >
            Thank you for any help
            >
            Use:
            >
            string text = "Good Morning";
            replaceStr(text ,"Morning","Eve ning");
            Console.WriteLi ne(text);
            >
            Output: Good Eorning // Only the first char
            >
            Here is the function
            >
            public static void replaceStr(stri ng word, string word2, string
            replace)
            {
            int i;
            int x = 0, y, z = 0, l = 0;
            char[] place = new char[word.Length];
            for (i = 0; i < word.Length; i++)
            {
            place[x] = word[i]; // fills the place Array
            x++;
            }
            >
            for (y = 0; y < place.Length; y++)
            {
            >
            if (place[y] == word2[z]) /
            {
            >
            place[y] = replace[l]; // It only replaces the
            first char
            l++;
            y++;
            z++;
            >
            }
            >
            }
            >
            Console.WriteLi ne(place);
            >
            }
            >
            That's because y is incremented twice on each interation of the loop.
            >
            Even after you fix that there's still some problems. First, it will
            replace things it should not be replacing. Second, strings are
            immutable so unless you return a new string that function is basically
            nothing more than a CPU consuming noop.- Zitierten Text ausblenden -
            >
            - Zitierten Text anzeigen -
            Thanks I fixed that with y but there other problems like you said. Any
            better implementation? I don't want to use the standard replace
            function

            Comment

            • Brian Gideon

              #7
              Re: My own version of replace

              On Nov 30, 7:26 am, implement <tomico...@gmai l.comwrote:
              My function works, but only if "word" has 11 chars? Why? If more chars
              then I got an System.IndexOut OfRangeExceptio n.
              You'll need to use a debugger and step through the code. Have you
              done that yet?

              Comment

              • implement

                #8
                Re: My own version of replace

                On 30 Nov., 15:13, Brian Gideon <briangid...@ya hoo.comwrote:
                On Nov 30, 7:26 am, implement <tomico...@gmai l.comwrote:
                >
                My function works, but only if "word" has 11 chars? Why? If more chars
                then I got an System.IndexOut OfRangeExceptio n.
                >
                You'll need to use a debugger and step through the code. Have you
                done that yet?
                Yes I used the debugger and it shows me that the place[x] array has 13
                elements but y starts with and is 12. I don't know why

                Comment

                • Brian Gideon

                  #9
                  Re: My own version of replace

                  On Nov 30, 8:20 am, implement <tomico...@gmai l.comwrote:
                  On 30 Nov., 15:13, Brian Gideon <briangid...@ya hoo.comwrote:
                  >
                  On Nov 30, 7:26 am, implement <tomico...@gmai l.comwrote:
                  >
                  My function works, but only if "word" has 11 chars? Why? If more chars
                  then I got an System.IndexOut OfRangeExceptio n.
                  >
                  You'll need to use a debugger and step through the code. Have you
                  done that yet?
                  >
                  Yes I used the debugger and it shows me that the place[x] array has 13
                  elements but y starts with and is 12. I don't know why
                  First, I don't see how the place array can have 13 elements since
                  "Good Morning" is only 12 characters. Have you changed your code?

                  Second, you're going to have to be more precise. The variable y
                  starts with...what? At what point is its value 12? What line is
                  throwing the IndexOutOfRange Exception.

                  If you've stepped through it in a debugger it should be easy to
                  identify why the exception is being generated. Is it because you're
                  indexing into the place array with a value greater than place.Length -
                  1? Remember, arrays are 0 based.

                  Comment

                  • implement

                    #10
                    Re: My own version of replace

                    On 30 Nov., 15:59, Brian Gideon <briangid...@ya hoo.comwrote:
                    On Nov 30, 8:20 am, implement <tomico...@gmai l.comwrote:
                    >
                    On 30 Nov., 15:13, Brian Gideon <briangid...@ya hoo.comwrote:
                    >
                    On Nov 30, 7:26 am, implement <tomico...@gmai l.comwrote:
                    >
                    My function works, but only if "word" has 11 chars? Why? If more chars
                    then I got an System.IndexOut OfRangeExceptio n.
                    >
                    You'll need to use a debugger and step through the code. Have you
                    done that yet?
                    >
                    Yes I used the debugger and it shows me that the place[x] array has 13
                    elements but y starts with and is 12. I don't know why
                    >
                    First, I don't see how the place array can have 13 elements since
                    "Good Morning" is only 12 characters. Have you changed your code?
                    >
                    Second, you're going to have to be more precise. The variable y
                    starts with...what? At what point is its value 12? What line is
                    throwing the IndexOutOfRange Exception.
                    >
                    If you've stepped through it in a debugger it should be easy to
                    identify why the exception is being generated. Is it because you're
                    indexing into the place array with a value greater than place.Length -
                    1? Remember, arrays are 0 based.
                    The Debugger stops at if (place[y] == word2[z]) with an
                    IndexOutOfRange Execption

                    Comment

                    • Tom Porterfield

                      #11
                      Re: My own version of replace

                      implement wrote:
                      The Debugger stops at if (place[y] == word2[z]) with an
                      IndexOutOfRange Execption
                      It appears you have changed the code since your original post. Can you
                      supply your current code as well as the inputs that you are passing into
                      your replaceStr method?
                      --
                      Tom Porterfield

                      Comment

                      • Tom Porterfield

                        #12
                        Re: My own version of replace

                        implement wrote:
                        The standard replace function is nice but I want to look behind the
                        screen how these things are implemented. I tried Reflector but it
                        doesn't show me the whole implementations . So I try to build these
                        functions again. Just for fun :-)
                        If it is the implementation that you are interested in, the following
                        downloads might be of value:

                        Shared Source Common Language Infrastructure 1.0 Release
                        http://www.microsoft.com/downloads/d...6-8DD34C6292F0

                        Shared Source Common Language Infrastructure 2.0 Release
                        http://www.microsoft.com/downloads/d...7-3121B4F51D4D
                        --
                        Tom Porterfield

                        Comment

                        • Tom Porterfield

                          #13
                          Re: My own version of replace

                          implement wrote:
                          I mean it's always easier replacing single chars than grouped
                          chars(strings)
                          Maybe so, but that isn't what string.Replace does so if you are trying
                          to reproduce the behavior of that you'll need to find the match across
                          the group and replace the group, taking into account that the number of
                          elements in the replacement group might be more or less than the number
                          of elements they are replacing. string.Replace also supports passing
                          null or an empty string as the "replace with" value, with the result
                          being that matched items are removed from the original string.
                          --
                          Tom Porterfield

                          Comment

                          Working...