My own version of replace

I

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; // 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);

}
 
F

Family Tree Mike

implement said:
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; // 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.
 
D

DeveloperX

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; // 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");
 
B

Brian Gideon

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; // 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.
 
I

implement

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

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; // 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
 
T

Tom Porterfield

implement said:
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.
 
I

implement

In order to provide recommendations on better implementation, please
help us understand why the standard replace function is not workable.

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 :)
 
B

Ben Voigt [C++ MVP]

implement said:
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.
 
I

implement

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

My function works, but only if "word" has 11 chars? Why? If more chars
then I got an System.IndexOutOfRangeException.
 
B

Brian Gideon

My function works, but only if "word" has 11 chars? Why? If more chars
then I got an System.IndexOutOfRangeException.

You'll need to use a debugger and step through the code. Have you
done that yet?
 
I

implement

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
 
B

Brian Gideon

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 IndexOutOfRangeException.

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.
 
I

implement

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 IndexOutOfRangeException.

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
IndexOutOfRangeExecption
 
T

Tom Porterfield

implement said:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption

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?
 
T

Tom Porterfield

implement said:
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/details.aspx?familyid=3A1C93FA-7462-47D0-8E56-8DD34C6292F0

Shared Source Common Language Infrastructure 2.0 Release
http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D
 
I

implement

implement said:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption

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?

Use:

string text = "Programming is fun";
replaceStr(text,"is","bla");

Debugger output:

place[x] contains 18 Elements, x too. place[x] contains these elements
in array order

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'P' 'r' 'o' 'g' 'r' 'a' 'm' 'm' 'b' 'n' 'g' ' ' 'i'
'l' ' ' 'f' 'u' 'n'

Now it goes to the next for loop

for (y = 0; y < place.Length; y++ ) // Debugger output: y = 14

Next:

if (place[y] == word2[z]) // here it crashes with an exception
18 14

I don't know why y is only 14. It should be 18 too?


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;
x++;
}



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

Console.WriteLine(y);

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


place[y] = replace[l];
l++;
z++;




}


}

Console.WriteLine(place);

}
 
T

Tom Porterfield

implement said:
implement said:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption
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?

Use:

string text = "Programming is fun";
replaceStr(text,"is","bla");

Debugger output:

place[x] contains 18 Elements, x too. place[x] contains these elements
in array order

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'P' 'r' 'o' 'g' 'r' 'a' 'm' 'm' 'b' 'n' 'g' ' ' 'i'
'l' ' ' 'f' 'u' 'n'

Now it goes to the next for loop

for (y = 0; y < place.Length; y++ ) // Debugger output: y = 14

Next:

if (place[y] == word2[z]) // here it crashes with an exception
18 14

What is 18 here??? At the point of the crash, y is 14 and z is 2.
I don't know why y is only 14. It should be 18 too?

It's 14 because it's not finished looping all the way through place. It
won't be 18 until the loop is finished, but you are crashing before that
happens.
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;
x++;
}

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

if (place[y] == word2[z])
{
place[y] = replace[l];
l++;
z++;
}
}
Console.WriteLine(place);
}


When I first looked at your code I suspected this would happen, but
didn't fire it up to confirm as others were on it. The issue for you is
that word2 is shorter than replace. You are incrementing the counter
for both word2 and replace everytime you do a substitution.

First of all, this will never work like string.Replace, but maybe that's
no longer a goal. As you can see from the output you pasted above, you
replaced the 'i' in "Programming" because it matched 'i' in "is" with
the 'b' in "bla", and then incremented the indexer for both "is" and
"bla". So now your code started looking for a match on 's' ("is"[1]).
It found a match for that in the 's' of "is" in your second word of
"Programming is fun" and replaced it with the 'l' in "bla" ("bla"[1]).
So you now have "Programmbng ls fun". You then again incremented the
indexer for both "is" and "bla" which means the next time through the
loop you are comparing place[14] with word2[2]. Since word2 = "is", it
has no character at index 2 (remember 0 based index) so you get your
argument out of range exception.

Hope this helps.
 
I

implement

implement said:
implement wrote:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption
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?

string text = "Programming is fun";
replaceStr(text,"is","bla");
Debugger output:
place[x] contains 18 Elements, x too. place[x] contains these elements
in array order
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'P' 'r' 'o' 'g' 'r' 'a' 'm' 'm' 'b' 'n' 'g' ' ' 'i'
'l' ' ' 'f' 'u' 'n'
Now it goes to the next for loop
for (y = 0; y < place.Length; y++ ) // Debugger output: y = 14

if (place[y] == word2[z]) // here it crashes with an exception
18 14

What is 18 here??? At the point of the crash, y is 14 and z is 2.
I don't know why y is only 14. It should be 18 too?

It's 14 because it's not finished looping all the way through place. It
won't be 18 until the loop is finished, but you are crashing before that
happens.




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;
x++;
}

for (y = 0; y < place.Length; y++ )
{
Console.WriteLine(y);
if (place[y] == word2[z])
{
place[y] = replace[l];
l++;
z++;
}
}
Console.WriteLine(place);
}

When I first looked at your code I suspected this would happen, but
didn't fire it up to confirm as others were on it. The issue for you is
that word2 is shorter than replace. You are incrementing the counter
for both word2 and replace everytime you do a substitution.

First of all, this will never work like string.Replace, but maybe that's
no longer a goal. As you can see from the output you pasted above, you
replaced the 'i' in "Programming" because it matched 'i' in "is" with
the 'b' in "bla", and then incremented the indexer for both "is" and
"bla". So now your code started looking for a match on 's' ("is"[1]).
It found a match for that in the 's' of "is" in your second word of
"Programming is fun" and replaced it with the 'l' in "bla" ("bla"[1]).
So you now have "Programmbng ls fun". You then again incremented the
indexer for both "is" and "bla" which means the next time through the
loop you are comparing place[14] with word2[2]. Since word2 = "is", it
has no character at index 2 (remember 0 based index) so you get your
argument out of range exception.

Hope this helps.
--
Tom Porterfield- Zitierten Text ausblenden -

- Zitierten Text anzeigen -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -


Thanks it helps but I don't know how can I change my false version
into a true one :-(.
 
I

implement

implement said:
implement wrote:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption
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
Use:
string text = "Programming is fun";
replaceStr(text,"is","bla");
Debugger output:
place[x] contains 18 Elements, x too. place[x] contains these elements
in array order
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'P' 'r' 'o' 'g' 'r' 'a' 'm' 'm' 'b' 'n' 'g' ' ' 'i'
'l' ' ' 'f' 'u' 'n'
Now it goes to the next for loop
for (y = 0; y < place.Length; y++ ) // Debugger output: y = 14
Next:
if (place[y] == word2[z]) // here it crashes with an exception
18 14
What is 18 here??? At the point of the crash, y is 14 and z is 2.
It's 14 because it's not finished looping all the way through place. It
won't be 18 until the loop is finished, but you are crashing before that
happens.
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;
x++;
}
for (y = 0; y < place.Length; y++ )
{
Console.WriteLine(y);
if (place[y] == word2[z])
{
place[y] = replace[l];
l++;
z++;
}
}
Console.WriteLine(place);
}

When I first looked at your code I suspected this would happen, but
didn't fire it up to confirm as others were on it. The issue for you is
that word2 is shorter than replace. You are incrementing the counter
for both word2 and replace everytime you do a substitution.
First of all, this will never work like string.Replace, but maybe that's
no longer a goal. As you can see from the output you pasted above, you
replaced the 'i' in "Programming" because it matched 'i' in "is" with
the 'b' in "bla", and then incremented the indexer for both "is" and
"bla". So now your code started looking for a match on 's' ("is"[1]).
It found a match for that in the 's' of "is" in your second word of
"Programming is fun" and replaced it with the 'l' in "bla" ("bla"[1]).
So you now have "Programmbng ls fun". You then again incremented the
indexer for both "is" and "bla" which means the next time through the
loop you are comparing place[14] with word2[2]. Since word2 = "is", it
has no character at index 2 (remember 0 based index) so you get your
argument out of range exception.
Hope this helps.
- Zitierten Text anzeigen -- Zitierten Text ausblenden -
- Zitierten Text anzeigen -

Thanks it helps but I don't know how can I change my false version
into a true one :-(.- Zitierten Text ausblenden -

- Zitierten Text anzeigen -


I think it's always easier replacing single chars as grouped
chars(strings)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top