Pagini recente » Cod sursa (job #857535) | Cod sursa (job #2200344)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 2000000
int min(int first, int second) {
if (first < second)
return first;
return second;
}
int main() {
char *first, *second;
unsigned long positions[1000];
int numberOfElements = 0;
int index = 0;
unsigned long position;
FILE *input = fopen("strmatch.in", "r");
FILE *output = fopen("strmatch.out", "w");
first = (char *)malloc(MAX_LENGTH * sizeof(char));
second = (char *)malloc(MAX_LENGTH * sizeof(char));
fscanf(input, "%s", first);
fscanf(input, "%s", second);
char *p = strstr(second, first);
while (p != NULL) {
position = p - second;
if (index < 1000) {
positions[index] = position;
index++;
}
numberOfElements++;
p = strstr(p + 1, first);
}
fprintf(output, "%d\n", numberOfElements);
int minValue = min(1000, numberOfElements);
for (int i = 0; i < minValue; i++) {
fprintf(output, "%lu ", positions[i]);
}
fclose(input);
fclose(output);
return 0;
}