Skip to content

Instantly share code, notes, and snippets.

@markogresak
Created May 13, 2014 13:27
Show Gist options
  • Save markogresak/3a023cbfd0e9ebafee53 to your computer and use it in GitHub Desktop.
Save markogresak/3a023cbfd0e9ebafee53 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct seznamBesed {
char **vrstice;
int stVrstic;
}; typedef struct seznamBesed* sBesed;
sBesed initSeznam(sBesed);
void dodajBesedo(sBesed, char *);
void preberiDatoteko(sBesed, char const *);
char firstChar(sBesed, int);
int main(int argc, char const *argv[]) {
if(argc != 4) exit(1);
sBesed a = NULL, b = NULL; a = initSeznam(a); b = initSeznam(b);
preberiDatoteko(a, argv[1]); preberiDatoteko(b, argv[2]);
for(int i = 0; i < a->stVrstic; i++)
for(int j = 0; j < b->stVrstic; j++) {
if(strcmp(a->vrstice[i], b->vrstice[j]) == 0) {
a->vrstice[i] = ""; b->vrstice[j] = "";
break;
}
if(i+1 < a ->stVrstic && strcmp(a->vrstice[i+1], b->vrstice[j]) == 0) {
a->vrstice[i+1] = ""; b->vrstice[j] = "";
break;
}
}
FILE *fout = fopen(argv[3], "w");
int max = a->stVrstic > b->stVrstic ? a->stVrstic : b->stVrstic;
for(int i = 0; i < max; i++) {
if(i < a->stVrstic && strcmp(a->vrstice[i], "") != 0) fputc(firstChar(a, i), fout);
if(i < b->stVrstic && strcmp(b->vrstice[i], "") != 0) fputc(firstChar(b, i), fout);
}
fputc('\n', fout);
fclose(fout); free(a); free(b);
return 0;
}
sBesed initSeznam(sBesed s) {
s = (sBesed) malloc(sizeof(struct seznamBesed));
s->vrstice = (char **) malloc(0); s->stVrstic = 0;
return s;
}
void dodajBesedo(sBesed s, char *beseda) {
s->vrstice = (char **) realloc(s->vrstice, sizeof(char *) * (s->stVrstic + 1));
s->vrstice[s->stVrstic] = (char *) malloc(sizeof(char) * (strlen(beseda) + 1));
strcpy(s->vrstice[s->stVrstic++], beseda);
}
void preberiDatoteko(sBesed s, char const *pot) {
FILE *f = fopen(pot, "r"); char *l = (char *) malloc(101);
while(fgets(l, 100, f) != NULL) dodajBesedo(s, l);
fclose(f); free(l);
}
char firstChar(sBesed s, int vrstica) { return s->stVrstic > vrstica ? *s->vrstice[vrstica] : '\0'; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment