Hi all. Is there a command in C that can delete a string contained in a longer string and then replace the deleted string with another? Big thanks to those who can help me with this.

Recommended Answers

All 20 Replies

No command. You have the write the code to do that yourself. Write down on a piece of paper the original string. Then using your pencil try to delete and insert the new text. Doing that visually may help you to understand more clearly how it might be done with a computer program. You will most likely need to use another chracter array for temporary storage of the string fragments.

No command. You have the write the code to do that yourself. Write down on a piece of paper the original string. Then using your pencil try to delete and insert the new text. Doing that visually may help you to understand more clearly how it might be done with a computer program. You will most likely need to use another chracter array for temporary storage of the string fragments.

OK, I have another problem though. The string that I have to delete and replace occurs several times in the longer string and I still can't figure out a way to make a loop to solve this. Could you give me an idea?

By the way, the longer string may vary in length, and the shorter string that I have to replace may occur several times in the longer string. So I really have to create a loop that will continuously search for the shorter string and replace all of its occurrences in the longer string.

pseudo code

do {
   does search string exist in long string?
   No -- then exit the loop
   replace search string with replacement string
} // end of loop

You should first try to write a string matching algorithm .

If the longer strings contains the smaller string in many places, can you successfully print the locations of the sub string in the main string ?

You should first try to write a string matching algorithm .

If the longer strings contains the smaller string in many places, can you successfully print the locations of the sub string in the main string ?

I think strstr() can already locate the substring, my real problems right now are:
1. creating a loop that continuously searches all occurrences of the substring until the end of the longer string
2. replace the substring with another string

You know what has to be done, so now all you have to do is stop asking the same question over and over and get to coding it. Its a very simple loop as previously illustrated.

You know what has to be done, so now all you have to do is stop asking the same question over and over and get to coding it. Its a very simple loop as previously illustrated.

Ancient Dragon, I wasn't asking a question. I just replied to the other person, and I wasn't even talking to you. Based from the other threads I created here, I noticed you get hot-headed too quickly, maybe you could just stop replying if you have no input to share. I appreciate your previous replies just as much as your silence when you're not the one being talked to.

commented: Great way to show your appreciation to those who take the time to help you. -3

Include string.h if you haven't already, then use strstr() which will return a pointer to the first letter of your sub string.

Now using that address, you can use memset() or memmove() to change the contents of the larger char array, to some sentinel value, blank spaces, or whatever you like.

Those are the two functions that will do the job for you. You will have to work with a char pointer or two, because that's what strstr() returns, not an index number. It's also what memmove() needs.

As mentioned above, you will need a loop, with the above.

Ancient Dragon, I wasn't asking a question. I just replied to the other person, and I wasn't even talking to you. Based from the other threads I created here, I noticed you get hot-headed too quickly, maybe you could just stop replying if you have no input to share. I appreciate your previous replies just as much as your silence when you're not the one being talked to.

It's just annoying when people give answers and the original poster, like you, just ignore those answers. Do you want someone to write the program for you? If you do then you are at the wrong web site. You have already been given enough ideas how to do it, so its up to you to get on with it and start coding. Once you do that you will understand the solution better.

It's just annoying when people give answers and the original poster, like you, just ignore those answers. Do you want someone to write the program for you? If you do then you are at the wrong web site. You have already been given enough ideas how to do it, so its up to you to get on with it and start coding. Once you do that you will understand the solution better.

Did I ask anyone, including you, that I want the code to be written for me? My question was very very simple and you can't even completely understand it? You are assuming that I want the code to be written for me when in fact I didn't even give a hint or say that explicitly. Simple question you can't understand, I can't believe you're in this forum. If you get annoyed too quickly, then you should be the first one to leave this web site. There are a lot of stupid web sites out there that can help make you happy. Here's my advice to you little whiner: stop whining, grow up, and [snip].

Also, you think everytime you post a reply in a thread you're already being able to help? Think again, in all honesty I found all your replies senseless. Again, my previous post you get mad at was NOT A QUESTION, IT WAS A REPLY TO ANOTHER PERSON. THAT SIMPLE.

Just because you're getting annoyed doesn't mean you should start posting stupid things against another poster. If not all people are talking to you, don't be sad. Deal with it.

commented: Very mature... -5

>>My question was very very simple and you can't even completely understand it?

Yes I have. I even posted some pseudo-code to show you how to do it in this link. And Adak gave you some suggestions here.

>>My question was very very simple and you can't even completely understand it?

Yes I have. I even posted some pseudo-code to show you how to do it in this link. And Adak gave you some suggestions here.

Just another post that offers nothing. Stop replying to me whiner.

commented: You don't deserve to be a programmer. Find another job. -2

1. creating a loop that continuously searches all occurrences of the substring until the end of the longer string
2. replace the substring with another string

Yes, this pretty much defines the whole problem. Have you tried anything yet? I still don't see any posted attempts by you, either in C or pseudocode. What kind of help are you expecting at this point? We don't even know where you are in terms of understanding the steps involved, and as such, the only conclusion is that you've done nothing in the hopes that someone will give you the solution.

So how about you stop whining and show some effort if you want continued help.

Include string.h if you haven't already, then use strstr() which will return a pointer to the first letter of your sub string.

Now using that address, you can use memset() or memmove() to change the contents of the larger char array, to some sentinel value, blank spaces, or whatever you like.

Those are the two functions that will do the job for you. You will have to work with a char pointer or two, because that's what strstr() returns, not an index number. It's also what memmove() needs.

As mentioned above, you will need a loop, with the above.

Thanks! In one post you were able to answer several questions.

Yes, this pretty much defines the whole problem. Have you tried anything yet? I still don't see any posted attempts by you, either in C or pseudocode. What kind of help are you expecting at this point? We don't even know where you are in terms of understanding the steps involved, and as such, the only conclusion is that you've done nothing in the hopes that someone will give you the solution.

So how about you stop whining and show some effort if you want continued help.

Where in my posts was I whining? This is just another reply that doesn't offer anything at all. I'm not yet posting my code here because it's not perfectly working, but I know I can handle the errors without any help yet.

Similar to what I told Ancient Dragon, if you have no input to share, why don't you just stop replying? You're just wasting my time reading at your useless comments, similar to Ancient Dragon's. Maybe you two are good buddies? Hmmm....

Thanks for confirming that I can ignore your pleas for help in the foreseeable future. You ask for help, but provide nothing to assist those kind enough to try. Then you bitch and moan when things don't go your way.

Grow up.

I never wanted to offend anyone, and I was never the first to initiate any negative comments here. I was trying to clarify my previous post but another poster insisted on arguing. I hope posters here who get really annoyed too quickly don't start arguments that can't be used to solve the question at all. Also, even if I don't post any code doesn't mean that I'm not exerting any efforts. I don't wanna ask everything so I first try to solve the errors as far as I can.

I never wanted to offend anyone, and I was never the first to initiate any negative comments here. I was trying to clarify my previous post but another poster insisted on arguing

IMO, it doesn't seem that way. Tip: if you don't find a particular reply suiting your taste, all you have to do it is ignore it. If you feel the reply is against the rules, just report it. It's really very simple. You started an argument by asking a particular forum member to "stop" contributing/posting to this thread, which you can't.

Regarding code request: even though it wasn't your intention to "ask" for code, failure to post code coupled with a lot of hand waving comes across as being lazy.

@shinsengumi:
One of the problems with an on-line forum is that you can't always tell what reply is meant for which poster.

Anything negative you read here, don't take it personally. Keep focused on the programming topic of the thread. Best course is simply the "water off a ducks back" philosophy, that S.O.S. mentioned above.

As far as Narue and Ancient Dragon, they both have excellent programming skill, (better than mine), and as your program comes together, you'll see that it uses a loop very much like the one that AD outlined in pseudo code for you.

I get the sense that you have a good deal of pride in yourself, and that's all OK, (clearly, if we aren't taking any pride in ourselves, who will?), but too much will hold you back from learning all that you could. It is the empty cup that will hold more fresh tea.

So, back to finding strings within a larger string. ;)

Where are you with it? You don't need to wait until it's working properly to post it, btw. If you're stuck on something, post up what it is, and tell what you're stuck on.

And let anything negative that you see, just roll right off your "oily feathers". Which is not always easy to do, I freely admit. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.