#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define STRING_LENGTH 1000000
#define TARGET "mouri"
void generate_random_string( char * str, size_t length) {
const char charset[ ] = "abcdefghijklmnopqrstuvwxyz" ;
for ( size_t i = 0 ; i < length; i++ ) {
int key
= rand ( ) % ( int ) ( sizeof ( charset
) - 1 ) ; str[ i] = charset[ key] ;
}
str[ length] = '\0 ' ;
}
int main( ) {
char * str
= ( char * ) malloc ( STRING_LENGTH
+ 1 ) ; if ( ! str) {
fprintf ( stderr
, "Memory allocation failed\n " ) ; return 1 ;
}
generate_random_string( str, STRING_LENGTH) ;
printf ( "Generated string: %.100s... (showing first 100 characters)\n " , str
) ;
char * ptr = str;
int count = 0 ;
while ( ( ptr
= strstr ( ptr
, TARGET
) ) != NULL
) { count++;
ptr++;
}
printf ( "The substring '%s' was found %d times in the generated string.\n " , TARGET
, count
) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBTVFJJTkdfTEVOR1RIIDEwMDAwMDAKI2RlZmluZSBUQVJHRVQgIm1vdXJpIgoKdm9pZCBnZW5lcmF0ZV9yYW5kb21fc3RyaW5nKGNoYXIgKnN0ciwgc2l6ZV90IGxlbmd0aCkgewogICAgY29uc3QgY2hhciBjaGFyc2V0W10gPSAiYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoiOwogICAgZm9yIChzaXplX3QgaSA9IDA7IGkgPCBsZW5ndGg7IGkrKykgewogICAgICAgIGludCBrZXkgPSByYW5kKCkgJSAoaW50KShzaXplb2YoY2hhcnNldCkgLSAxKTsKICAgICAgICBzdHJbaV0gPSBjaGFyc2V0W2tleV07CiAgICB9CiAgICBzdHJbbGVuZ3RoXSA9ICdcMCc7Cn0KCmludCBtYWluKCkgewogICAgY2hhciAqc3RyID0gKGNoYXIgKiltYW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