fork(2) download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <memory.h>
  6.  
  7. char * replacecstr(char *str, char*substri, char* replacement);
  8.  
  9. /* Here is algorithmic overview.
  10.   1. Copy byte by byte
  11. 2. If possible substring-start is found, stop copying, note position
  12. 3. If substring match continues, continue comparing;
  13. 3.1 If it fails to match the substring, put us back just after we thought it was a substring
  14. 3.2 If the substring is a match, write the replacement instead
  15. 4. Else, goto 1
  16. */
  17.  
  18. char * replacecstr(char *str, char*substri, char* replacement)
  19. {
  20. enum states { searching, possible };
  21.  
  22. char *substr = substri;
  23. char *curptr = str;
  24. char *possible_start = str;
  25.  
  26. char *first_malloc = (char*)malloc(strlen(str)*10);
  27.  
  28. char *writepos = first_malloc;
  29. states status = searching;
  30. for (;;) {
  31.  
  32. // **** This part checks if we're at the end of the string, and handles corner-cases
  33. if (*curptr == 0) { // End of input string
  34. if (status==possible) { // If we thought we might be in a substring...are we?
  35. if (*substr == 0) { // We've matched our substring with our string right at the end, the corner case, so do our replacement
  36. char *tmp = replacement;
  37. while(*tmp!=0) {
  38. *writepos = *tmp;
  39. writepos++;
  40. tmp++;
  41. }
  42. }
  43. else { // We thought we were in a substring but we're not, so we need to copy what we missed
  44. while (*possible_start) {
  45. *writepos = *possible_start;
  46. possible_start++;
  47. writepos++;
  48. }
  49. }
  50. }
  51. break; // We're at the end no matter what, so get out of this loop
  52. }
  53.  
  54. // **** This part covers what to do when we think we're in a substring
  55. if (status==possible) {
  56. if (*substr == 0) // Reached end of substring, found a match!
  57. {
  58. status = searching;
  59. char *tmp = replacement;
  60. while(*tmp!=0) {
  61. *writepos = *tmp;
  62. writepos++;
  63. tmp++;
  64. }
  65. substr = substri;
  66. continue;
  67. }
  68. else if (*curptr!=*substr) { // Substring doesn't match, time to go back to just after the possible start to see if we have a substring
  69.  
  70. status = searching;
  71. curptr = possible_start + 1;
  72. *writepos = *possible_start;
  73. writepos++;
  74. substr = substri;
  75. continue;
  76. }
  77. else { // Just another part of the substring
  78. curptr++;
  79. substr++;
  80. continue;
  81. }
  82. }
  83.  
  84. // **** This part checks if we have a possible start to the sub-string
  85. if (*curptr==*substr) {
  86. status = possible;
  87. possible_start = curptr;
  88. curptr++;
  89. substr++;
  90. continue;
  91. }
  92.  
  93. // **** Thos part continues the byte-by-byte copy
  94. *writepos = *curptr;
  95. writepos++;
  96. curptr++;
  97. }
  98.  
  99. *writepos = 0;
  100.  
  101. int tcpy = strlen(first_malloc)+1;
  102. char *malloc2 = (char *)malloc(tcpy);
  103. memcpy(malloc2,first_malloc,tcpy);
  104. free(first_malloc);
  105. return malloc2;
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111. char *foo = replacecstr("Hello bob how bbob are you","bob","ann");
  112. std::cout << foo;
  113. free(foo);
  114.  
  115. char foobarre[500];
  116. gets(foobarre);
  117. }
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
Hello ann how bann are you