Home All Groups Group Topic Archive Search About

My own version of replace

Author
29 Nov 2007 5:08 PM
implement
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","Evening");
Console.WriteLine(text);

Output: Good Eorning // Only the first char

Here is the function

public static void replaceStr(string 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.WriteLine(place);

}

Author
29 Nov 2007 5:24 PM
Family Tree Mike
Show quote
"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","Evening");
> Console.WriteLine(text);
>
> Output: Good Eorning // Only the first char
>
> Here is the function
>
> public static void replaceStr(string 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.WriteLine(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","Abend");
Console.WriteLine(text);

You start to replace the word Morgen before completely checking if it equals
Morning.
Author
29 Nov 2007 5:25 PM
DeveloperX
On 29 Nov, 17:08, implement <tomico***@gmail.com> wrote:
Show quote
> 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","Evening");
> Console.WriteLine(text);
>
> Output: Good Eorning // Only the first char
>
> Here is the function
>
> public static void replaceStr(string 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.WriteLine(place);
>
>  }

You've double incremented y. There's a second issue you'll find when
you try the test line with

replaceStr("GoMod Morning", "Morning", "Evening");
Author
29 Nov 2007 5:33 PM
Brian Gideon
On Nov 29, 11:08 am, implement <tomico***@gmail.com> wrote:
Show quote
> 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","Evening");
> Console.WriteLine(text);
>
> Output: Good Eorning // Only the first char
>
> Here is the function
>
> public static void replaceStr(string 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.WriteLine(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.
Author
29 Nov 2007 5:46 PM
implement
On 29 Nov., 18:33, Brian Gideon <briangid***@yahoo.com> wrote:
Show quote
> On Nov 29, 11:08 am, implement <tomico***@gmail.com> 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","Evening");
> > Console.WriteLine(text);
>
> > Output: Good Eorning // Only the first char
>
> > Here is the function
>
> > public static void replaceStr(string 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.WriteLine(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
Author
29 Nov 2007 5:52 PM
Tom Porterfield
implement wrote:
> 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

In order to provide recommendations on better implementation, please
help us understand why the standard replace function is not workable.
--
Tom Porterfield
Author
29 Nov 2007 6:00 PM
implement
On 29 Nov., 18:52, Tom Porterfield <tppor***@mvps.org> wrote:
> implement wrote:
> > 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
>
> In order to provide recommendations on better implementation, please
> help us understand why the standard replace function is not workable.
> --
> Tom Porterfield

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 :-)
Author
29 Nov 2007 7:04 PM
Ben Voigt [C++ MVP]
Show quote
"implement" <tomico***@gmail.com> wrote in message
news:6d127aa9-b57b-4f05-86a3-0c240ee270eb@s12g2000prg.googlegroups.com...
> On 29 Nov., 18:52, Tom Porterfield <tppor***@mvps.org> wrote:
>> implement wrote:
>> > 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
>>
>> In order to provide recommendations on better implementation, please
>> help us understand why the standard replace function is not workable.
>> --
>> Tom Porterfield
>
> 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 :-)

You should start by describing the exact behavior of the function.

The code you gave would be described as: "looks through the input until it
starts finding the findtext, then starts replacing with the newtext", which
is not what the built-in replace does.
Author
29 Nov 2007 5:41 PM
Peter Bromberg [C# MVP]
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."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com



Show quote
"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","Evening");
> Console.WriteLine(text);
>
> Output: Good Eorning // Only the first char
>
> Here is the function
>
> public static void replaceStr(string 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.WriteLine(place);
>
>  }
>

AddThis Social Bookmark Button