Cod sursa(job #2324544)

Utilizator AxellApostolescu Alexandru Axell Data 20 ianuarie 2019 23:04:15
Problema Potrivirea sirurilor Scor 10
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <stdio.h>
#include <string.h>
#define LENGHT 100
#define SIZE 100

void apparitions(char *a, char *b, FILE* out);

int main() {
	FILE* in = fopen("strmatch.in", "rt");
	if (in == NULL) {
		printf("Couldn`t open input file!\n");
		return -1;
	}
	FILE* out = fopen("strmatch.out", "wt");
	if (out == NULL) {
		printf("Couldn`t open output file!\n");
		return -2;
	}
	char a[LENGHT], b[LENGHT];
	fgets(a, LENGHT - 1, in);
	char *pos;
	if ((pos=strchr(a, '\n')) != NULL) {
		*pos = '\0';
	}
	if ((pos=strchr(b, '\n')) != NULL) {
		*pos = '\0';
	}
	fgets(b, LENGHT - 1, in);
	apparitions(a, b, out);
	fclose(in);
	fclose(out);
	return 0;
}

void apparitions(char *a, char *b, FILE* out) {
	int occur[SIZE], ct = 0, yes = 0;
	if (strlen(a) > strlen(b)) {
		return;
	}
	for (int i = 0 ; i < (int)strlen(b) ; ++i) {
		for (int j = 0 ; j < (int)strlen(a) ; ++j) {
			if (a[j] == b[i + j]) {
				yes++;
			}
		}
		if (yes == (int)strlen(a)) {
			occur[ct] = i;
			ct++;
		}
		yes = 0;
	}
	fprintf(out, "%d\n", ct);
	for (int i = 0 ; i < ct ; ++i) {
		fprintf(out, "%d ", occur[i]);
	}
}