fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::string;
  9.  
  10. const char* Find(const char *szOrig, const char *szSearch)
  11. {
  12. const char *szResult = szOrig;
  13. while (*szResult && strlen(szResult) >= strlen(szSearch))
  14. {
  15. int r = strncmp(szResult, szSearch, std::min(strlen(szSearch), strlen(szResult)));
  16. //cout << r << " - " << int(szResult - szOrig) << endl;
  17. if (!r) break;
  18. szResult++;
  19. }
  20. return *szResult ? (strlen(szResult) >= strlen(szSearch) ? szResult : NULL) : NULL;
  21. }
  22.  
  23. char* Replace(const char *szOrig, const char *szSearch, const char *szReplace)
  24. {
  25. unsigned int maxMatches = strlen(szOrig) / strlen(szSearch);
  26. const char **arrMatches = new const char*[maxMatches];
  27. unsigned int matches = 0;
  28. const char *szResult = szOrig;
  29. while (szResult)
  30. {
  31. szResult = Find(szResult, szSearch);
  32. if (szResult)
  33. {
  34. arrMatches[matches++] = szResult;
  35. szResult += strlen(szSearch);
  36. }
  37. }
  38. char *szNew = NULL;
  39. if (matches)
  40. {
  41. cout << "Matches: " << matches << endl;
  42. //Calculate the new string size.
  43. unsigned int newSize = strlen(szOrig) + matches * (strlen(szReplace) - strlen(szSearch)) + 1;
  44. cout << "New size: " << newSize << endl;
  45. szNew = new char[newSize];
  46. //memset(szNew, 0, newSize);
  47. char *szCurPos = szNew;
  48. //Concatenate
  49. const char *szStart = szOrig;
  50. for(unsigned int i = 0; i < matches; i++)
  51. {
  52. strncpy(szCurPos, szStart, arrMatches[i] - szStart);
  53. szCurPos += arrMatches[i] - szStart;
  54. strncpy(szCurPos, szReplace, strlen(szReplace));
  55. szCurPos += strlen(szReplace);
  56. szStart = arrMatches[i] + strlen(szSearch);
  57. }
  58. //Copy any remainder and null-terminate the string.
  59. if (szStart < szOrig + strlen(szOrig))
  60. {
  61. strncpy(szCurPos, szStart, strlen(szStart));
  62. }
  63. szNew[newSize - 1] = '\0';
  64. }
  65. delete[] arrMatches;
  66. return szNew;
  67. }
  68.  
  69. int main()
  70. {
  71. string strSentence;
  72. string strSearch;
  73. string strReplace;
  74. getline(cin, strSentence);
  75. getline(cin, strSearch);
  76. getline(cin, strReplace);
  77. cout << "Sentence: " << strSentence << " - " << strSentence.size() << " characters." << endl;
  78. cout << "Search string: " << strSearch << " - " << strSearch.size() << " characters." << endl;
  79. cout << "Replace string: " << strReplace << " - " << strReplace.size() << " characters." << endl;
  80. //const char *szResult = Find(strSentence.c_str(), strSearch.c_str());
  81. //cout << (szResult ? szResult : "No matches found.") << endl;
  82. char *szNewString = Replace(strSentence.c_str(), strSearch.c_str(), strReplace.c_str());
  83. if (szNewString)
  84. {
  85. cout << "New String (" << strlen(szNewString) << " chars): " << endl << szNewString << endl;
  86. delete[] szNewString;
  87. }
  88. return 0;
  89. }
Success #stdin #stdout 0s 2864KB
stdin
Microsoft Active Server Pages (ASP) is a server-side scripting environment that you can use to create interactive Web pages and build powerful Web applications. When the server receives a request for an .asp file, it processes server-side scripts contained in the file to build the Web page that is sent to the browser.
server
SERVIDOR
stdout
Sentence:  Microsoft Active Server Pages (ASP) is a server-side scripting environment that you can use to create interactive Web pages and build powerful Web applications. When the server receives a request for an .asp file, it processes server-side scripts contained in the file to build the Web page that is sent to the browser. - 319 characters.
Search string:  server - 6 characters.
Replace string:  SERVIDOR - 8 characters.
Matches: 3
New size: 326
New String (325 chars):  
Microsoft Active Server Pages (ASP) is a SERVIDOR-side scripting environment that you can use to create interactive Web pages and build powerful Web applications. When the SERVIDOR receives a request for an .asp file, it processes SERVIDOR-side scripts contained in the file to build the Web page that is sent to the browser.