fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. const char* beforePrefix = "asdfasdfasdfasdf";
  10. const char* prefix = "<!doctortype html";
  11. const char* suffix = ".html>";
  12. const char* postSuffix = "asdasdasd";
  13.  
  14. unsigned size = 1024;
  15. char buf[size];
  16. sprintf(buf, "%s%sTHE STRING YOU WANT TO GET%s%s", beforePrefix, prefix, suffix, postSuffix);
  17.  
  18. cout << "Before: " << buf << endl;
  19.  
  20. const char* firstOccurenceOfPrefixPtr = strstr(buf, prefix);
  21. const char* firstOccurenceOfSuffixPtr = strstr(buf, suffix);
  22.  
  23. if (firstOccurenceOfPrefixPtr && firstOccurenceOfSuffixPtr)
  24. {
  25. unsigned textLen = (unsigned)(firstOccurenceOfSuffixPtr - firstOccurenceOfPrefixPtr - strlen(prefix));
  26. char newBuf[size];
  27. strncpy(newBuf, firstOccurenceOfPrefixPtr + strlen(prefix), textLen);
  28. newBuf[textLen] = 0;
  29.  
  30. cout << "After: " << newBuf << endl;
  31. }
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Before: asdfasdfasdfasdf<!doctortype htmlTHE STRING YOU WANT TO GET.html>asdasdasd
After: THE STRING YOU WANT TO GET