Jump to content
  • Advertisement
Sign in to follow this  
suliman

c++: replace word in char array?

This topic is 734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I need something like this in c++:

char cArray[40];
set it to 'My name is X1. I am MOOD'

myReplace(cArray,'X1','Billy');
myReplace(cArray,'MOOD','very sad');

cArray is now
'My name is Billy. I am very sad'

But using only char array (no string class or helper stuff). Any help is appiciated!

Erik

Share this post


Link to post
Share on other sites
Advertisement

Does "no helper stuff" include STL-libaries? If not, then you could use std::regex, which will also work just with char-arrays:

 

http://en.cppreference.com/w/cpp/regex

 

If you cannot use those eigther, you'll just have to write a routine that checks for occurances of a substring in a string yourself, like you already showed with myReplace. Did you try to implement it, and if yes, what is it that you need help with. If not, why?

Share this post


Link to post
Share on other sites

Are you guaranteed to have the same length of array both before and after?  Otherwise, I'm not exactly sure how you want this to work, you're likely to overflow the array.  (Also, I really suggest just using String type so you don't have to deal with this stuff)

 

There is probably a way to do this inline, but the easiest way would be to copy a second array, walk along and replace the word, but keep a pointer to the original untouched version  at the end of the replaced word and copy that in over the first array at the point of the end of the inserted word.  

Share this post


Link to post
Share on other sites

Sounds like a homework assignment to me, so I'll try to be vague.

 

If you are not allowed to use existing helper functions, you have to implement your own.

 

To solve this you need 3 functions:

1) find the length of a string, like strlen

2) find a string inside a string, like strstr

3) copy a part of one string to another, like strncpy

 

If you are stuck with the interface you described and have to work with that one char array given, you'll need to be extra careful not to stomp over input you still need.

 

I hope that helps.

Share this post


Link to post
Share on other sites
That's a neat little homework project with all kinds of fun potential 'gotcha' elements.

Fixed buffer lengths, expansion/contraction of data, and steps that can break each other. Forbidding you from using existing libraries for string management or regex replacement makes it even more fun.

I approve of your teacher. I hope they test with names like X1 becomes MOOD, then replace MOOD to whatever, or replace with a name like aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa. There are several good real-world issues that could be taught with that example.

Share this post


Link to post
Share on other sites

No assignment:)

Ill just do it with brute force and for loops then:P

I thought there would be some more clever trick.

Share this post


Link to post
Share on other sites
I thought there would be some more clever trick.

 

Don't expect any magic. There are actually some extremely clever tricks for string manipulation, like suffix arrays and trees, but those only come in handy if you have extremely large strings, probably append data often and have to search for certain occurances of substrings repeatedly (and they also don't go well with replacing stuff in the middle). So yeah, most "clever tricks" will deal with guaranteeing that your algorithm will scale at least something like logarithmically with the amount of data you add (instead of becoming O(n^2)), and will probably not even pay out if you have to perform operations on a small set of data.

Edited by Juliean

Share this post


Link to post
Share on other sites

I thought there would be some more clever trick.

 

The clever trick is using the right tool for the right job.  :P

(that is to say, ditching c-strings except where they are genuinely required)

Share this post


Link to post
Share on other sites

But using only char array (no string class or helper stuff).


By the pricking of my thumbs, buffer overflows - here they comes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!