I am trying to replace asterisk to some words in a string. e.g: if the string is 'A big giant person scared me', and i need the words 'giant' and 'scared' to be replaced with asterisk. the string should be 'A big ***** person ****** me'
here is my code
int lengthString(char* str) {
int i = 0;
while (str[i] != '\0')
i++;
return i;
}
void replace(char* start, int length, char ch) {
for (int i = 0; i < length; i++)
start[i] = ch;
}
void redact(char* toRedact, char** dictonary, int dicSize) {
for (int i = 0; i < dicSize; i++) {
char* redact = dictonary[i];
char* lastPos = toRedact;
char* nextPos = strstr(lastPos, redact);
while (lastPos != NULL) {
if (nextPos == NULL){
break;
}
replace(nextPos, lengthString(redact), '*');
lastPos = nextPos + 1;
}
}
}
int main() {
char* test[4];
char* dictionary[4];
dictionary[0] = "big";
dictionary[1] = "giant";
dictionary[2] = "scared";
dictionary[3] = "person";
test[0] = "A giant scared me";
test[1] = "A big scared me";
test[2] = "A person scared me";
for(int i = 0; i < 3; i++) {
redact(test[i], dictionary, 4);
printf("%s\n", test[i]);
}
return 0;
}
output: seg fualt