473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

}
Nov 29 '07 #1
12 2085
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");

Nov 29 '07 #2


"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.
Nov 29 '07 #3
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.
Nov 29 '07 #4
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

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

}
Nov 29 '07 #5
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

Nov 29 '07 #6
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?
Nov 30 '07 #7
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
Nov 30 '07 #8
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.
Nov 30 '07 #9
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
Nov 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3270
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
0
1448
by: ianstratford | last post by:
I just released a new version of the free Microsoft Access find and replace add-in utility (version 1.5). Search and replace (optionally) any string in tables, queries, macros, forms, reports and modules in Access databases. The tool will now search and replace Report Group Levels (Thank you Judy). The new version also searches macros (logs results but does not
0
2016
by: Neil Sargent | last post by:
This is a general posting of how I fixed a problem created by using the Access 97 ODE Setup Wizard on a Windows XP machine. I hope it helps anyone who comes across the problem saves them the 3 days its taken me to sort out. The application generates the following error: "Incompatible version of the RPC stub" This occurs when trying to run Outlook to send an email. However I expect it would occur for any operation which uses...
5
1897
by: Tara via AccessMonster.com | last post by:
Hi there - I'll do my best to explain my dilema. I'm using Access 2000. In this database, there is one table with about 150 columns of information, and 206 rows. There are numerous queries and reports. I've set up input forms for staff to enter data easily. There are 8 staff members. My boss wants to ensure that data entered into this database is correct right off the bat. She's asking for a program or code to do the following:
1
6215
by: Jim | last post by:
Hi. I would like to validate the version string that I get from FileVersionInfo. Is there a way via the object model to do this? I am currently doing this: FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath); string fileVersion = fileVersionInfo.FileVersion;
5
5101
by: Bazza Formez | last post by:
Greetings... I have an odd problem to do with replacing a DLL. My ASP.NET application makes use of a 3rd party charting component. This component is supplied as a DLL file. The component has worked well for us, but now I am having real problems trying to replace the DLL with a new version.
8
7775
by: Rak | last post by:
I am looking for a way to programatically change the .net version of the virtual directory that I am creating within a aspx page. As part of creating a new customer in my asp.net 2 application, it automatically creates a virtual directory and configures it. I am using the DirectoryServices.DirectoryEntry class in C# to do this. I am unable to set the .Net version of the virtual directory from 1.1 to version 2. All searches led me to the...
2
4925
by: ssp | last post by:
Hello there, I am trying to do very simple thing in trying to return results from an Oracle (10g) Stored Procedure using the Enterprise Library June 2005 Version. The stored procedure first: ============================================ CREATE OR REPLACE PROCEDURE AGENCYSELECTALL(p_cursor OUT SelectPackage.SelectCursor)
6
2597
by: John | last post by:
I have written a VB.NET DLL that is called by a third party program. If I make any changes in the DLL and then try to replace my DLL file the third party program will abort, saying that the DLL version number does not match to the DLL that it was linked against. How can I recompile my DLL without incrementing the version number? I don't see where this "version number" is stored in the project. The version number, under the "Assembly...
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.