I'm trying to do an interesting exercise from a book with the following question:
For our dynamically allocated strings, create a function replaceString that takes three parameters, each of type arrayString: source, target, and replaceText. The function replaces every occurrence of target in source with replaceText. For example, if source points to an array containing abcdabee, target points to ab, and replaceText points to xyz, then when the function ends, source should point to an array containing xyzcdxyzee.
I replace every occurence of target, in this case ab, with replaceText, in this case xyz in char *.
For this exercise the book uses a 0 to mark char * termination and uses a typedef for char *. I've written the following function to test replaceString out.
arrayString being typedef char *arrayString
void replaceStringTester() {
arrayString test = new char[8];
test[0] = 'a';
test[1] = 'b';
test[2] = 'c';
test[3] = 'd';
test[4] = 'a';
test[5] = 'b';
test[6] = 'e';
test[7] = 'e';
test[8] = 0;
arrayString target = new char[3];
target[0] = 'a';
target[1] = 'b';
target[3] = 0;
arrayString replaceText = new char[4];
replaceText[0] = 'x';
replaceText[1] = 'y';
replaceText[2] = 'z';
replaceText[3] = 0;
replaceString(test, target, replaceText);
cout << test;
}
As well as the following function to find the length of an arrayString
int length(arrayString as) {
int count = 0;
while (as[count] != 0) {
count;
}
return count;
}
My idea up until now has been, iterate over the source arrayString, check does the source start with the first char of target? If it does, iterate over target and check if the rest lines up. I've written following function for it, however im uncertain if it does exactly what I drafted up.
void replaceString(arrayString &source, arrayString target, arrayString replaceText) {
int sourceLength = length(source);
int targetLength = length(target);
int replaceTextLength = length(replaceText);
int targetPresent = 0;
for (int i = 0; i < sourceLength; i) {
if (source[i] == target[0]) {
int count = 0;
for (int k = 0; k < targetLength; k) {
if (target[k] == source[i k]) {
count;
}
}
if (count == targetLength) {
targetPresent;
}
}
}
int newStringLength = sourceLength (replaceTextLength - targetLength) * targetPresent 1;
arrayString newString = new char[newStringLength];
newString[newStringLength] = 0;
int j = 0;
int i = 0;
while (j < newStringLength) {
if (source[j] == target[0]) {
bool targetAcquired = false;
for (int k = 0; k < targetLength; k) {
if (target[k] == source[j k]) {
targetAcquired = true;
} else {
targetAcquired = false;
break;
}
}
if (targetAcquired) {
for(int k = 0; k < replaceTextLength; k) {
newString[i] = replaceText[k];
i;
}
j = targetLength;
}
if (!targetAcquired) {
newString[i] = source[j];
j;
i;
}
} else {
newString[i] = source[j];
j;
i;
}
}
delete[] source;
source = newString;
}
Edit:
I've solved it by implementing two trackers for our positions in each respective stringArray and then filtering with a boolean what is in where. Thank you for your help.
CodePudding user response:
For starters using this typedef
typedef char *arrayString;
is a bad idea. For example if you need to declare a pointer to a constant string then this declaration
const arrayString p;
will not denote
const char *p;
It means
char * const p;
that is not the same as the above declaration..
And the second and the third parameters of your function should be declared as having the type const char *
because they are not changed within the function,
The function should be declared the following way
char * replaceString( char * &source, const char *target, const char *replaceText );
Within the function you need at first to count how many times the string target
is found in the string source
. When using this information and the length of the string replaceText
you need to allocate dynamically a new character array if it is required. When just copy the source string into the dynamically allocated array substituting the target string for the replacing string.
After that you should delete the source string and assign its pointer with the newly formed string.
Pat attention to that there is standard C string function strstr
declared in the header <cstring>
that can simplify your code. Also to find the length of a string you can use another standard C string function strlen
. Otherwise you should write them yourself.
CodePudding user response:
Count how many elements you've assigned:
test[0] = 'a'; // 1 test[1] = 'b'; // 2 test[2] = 'c'; // 3 test[3] = 'd'; // 4 test[4] = 'a'; // 5 test[5] = 'b'; // 6 test[6] = 'e'; // 7 test[7] = 'e'; // 8 test[8] = 0; // 9
You've assigned 9 elements. Are there 9 elements in the array?
arrayString test = new char[8];
No. There aren't 9 elements in the array. There are 8. You access outside the bounds of the array and the behaviour of the program is undefined.
arrayString being typedef char *arrayString
Don't use obfuscation like this. It reduces readability and provides no advantages.