Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. char* myreplaceall(char* str, char* suf1, char* suf2) {
  2. char* res = (char*)malloc(sizeof(char*));
  3. res[0] = '\0';
  4. int i = 0; int i2 = 0; int j = 0;
  5. while (i < strlen(str)) {
  6. while ((str[i+j] == suf1[0+j]) & (j != strlen(suf1))) j++;
  7. if (j == strlen(suf1)){
  8. res = realloc(res, i2 + strlen(suf2));
  9. for (int k = 0; k < strlen(suf2); k++) res[i2 + k] = suf2[0 + k];
  10. i = i + strlen(suf1);
  11. i2 = i2 + strlen(suf2);
  12. }
  13. else {
  14. res = realloc(res, i2 + 1);
  15. res[i2] = str[i];
  16. i++; i2++;
  17. }
  18. j = 0;
  19. }
  20. res[i2] = '\0';
  21. return res;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement