+ 2
Please help
*Can't understand line 44, why he did this *And 41 too why (pos-str) and not (str-pos) https://sololearn.com/compiler-playground/c6RKCv8aK64c/?ref=app
3 Antworten
+ 1
Line 41 calculates the index by subtracting `str` from `pos` because `pos` is the starting address of the substring found in `str`. Subtracting `str` from `pos` gives the index or position of `old_w` in the original string `str`.
In line 44, the purpose of `strcat(str, temp+index+owlen)` is to concatenate the remaining part of the original string `str` after the occurrence of `old_w` (starting from `index + owlen`) to the modified `str` after replacing `old_w` with `new_w`.
Here,
- `temp` is a buffer where a copy of the string `str` is stored for manipulation.
- `index` represents the index or position in the original string where `old_w` was found.
- `owlen` is the length of the string `old_w`.
- `temp+index+owlen` points to the location in `temp` immediately after `old_w` ends.
+ 1
Cpp