c++: replace word in char array?

Started by
7 comments, last by Khatharr 7 years, 9 months ago

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

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?

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.

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.

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.

No assignment:)

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

I thought there would be some more clever trick.

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.

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)

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


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

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement