fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. #define STRING_LENGTH 1000000
  7. #define TARGET "mouri"
  8.  
  9. void generate_random_string(char *str, size_t length) {
  10. const char charset[] = "abcdefghijklmnopqrstuvwxyz";
  11. for (size_t i = 0; i < length; i++) {
  12. int key = rand() % (int)(sizeof(charset) - 1);
  13. str[i] = charset[key];
  14. }
  15. str[length] = '\0';
  16. }
  17.  
  18. int main() {
  19. char *str = (char *)malloc(STRING_LENGTH + 1);
  20. if (!str) {
  21. fprintf(stderr, "Memory allocation failed\n");
  22. return 1;
  23. }
  24.  
  25. srand(time(NULL));
  26. generate_random_string(str, STRING_LENGTH);
  27.  
  28. printf("Generated string: %.100s... (showing first 100 characters)\n", str);
  29.  
  30. char *ptr = str;
  31. int count = 0;
  32. while ((ptr = strstr(ptr, TARGET)) != NULL) {
  33. count++;
  34. ptr++;
  35. }
  36.  
  37. printf("The substring '%s' was found %d times in the generated string.\n", TARGET, count);
  38.  
  39. free(str);
  40. return 0;
  41. }
Success #stdin #stdout 0.02s 5284KB
stdin
Standard input is empty
stdout
Generated string: keaphkibkeobamkkbrsuxwncmbptvphivhxctfeeluhnhrziischqrjctyvqpcyklxpfctlpnscwmceuwicoanqvooldqmqejfjo... (showing first 100 characters)
The substring 'mouri' was found 0 times in the generated string.