Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int compare(const void* a, const void* b)
- {
- return (*(int*)a - * (int*)b);
- }
- char* read_del(char* pchar)
- {
- char temp[15]; //array temporar pentru a citi o linie
- fgets(temp, 15, stdin); //citirea liniei
- int lg = strlen(temp);
- pchar = (char*)malloc(lg); //alocam memorie dinamic cu lungimea lui temp
- strcpy(pchar, temp); //copiem in array-ul alocat dinamic
- return strdup(pchar); //returnam linia citita
- }
- char* read_out_del(char *pchar)
- {
- char temp[15]; //array temporar pentru a citi o linie
- fgets(temp, 15, stdin); //citirea liniei
- int lg = strlen(temp) - 1;
- temp[lg] = 0; //scapam de \n din final
- pchar = (char*)malloc(lg); //alocam memorie dinamic cu lungimea lui temp
- strcpy(pchar, temp); //copiem in array-ul alocat dinamic
- return strdup(pchar); //returnam linia citita
- }
- char* read_line(char* pchar)
- {
- char temp[200]; //array temporar pentru a citi o linie
- fgets(temp, 200, stdin); //citirea liniei
- int lg = strlen(temp) - 1;
- temp[lg] = 0; //scapam de \n din final
- pchar = (char*)malloc(lg); //alocam memorie dinamic cu lungimea lui temp
- strcpy(pchar, temp); //copiem in array-ul alocat dinamic
- return strdup(pchar); //returnam linia citita
- }
- void read_column(int *arr, int *n)
- {
- int i, k = 0;
- for(i = 0; i < (*n); ++i)
- scanf("%d,", &arr[i]);
- qsort(arr, (*n), sizeof(int), compare);
- for(i = 1; i < (*n); ++i)
- if(arr[k] != arr[i])
- arr[++k] = arr[i];
- *n = k + 1;
- }
- int main()
- {
- int *column, n, m, i, lgout_del;
- char *del, *out_del, temp[2000];
- //
- del = 0;
- del = read_del(del);
- //
- scanf("%d\n", &n);
- column = malloc(n * sizeof(int));
- read_column(column, &n);
- scanf("\n");
- //
- out_del = 0;
- out_del = read_out_del(out_del);
- lgout_del = strlen(out_del);
- //
- scanf("%d\n", &m);
- char **text = malloc(m * sizeof(*text));
- for(i = 0; i < m; ++i)
- text[i] = read_line(text[i]);
- //
- for(i = 0; i < m; ++i)
- {
- int nrcuv = 0, k = 0;
- strcpy(temp, "");
- char *pchar = strtok(text[i], del);
- while(pchar != NULL)
- {
- if(++nrcuv == column[k])
- {
- strcat(temp, pchar);
- strcat(temp, out_del);
- ++k;
- }
- pchar = strtok(NULL, del);
- }
- temp[strlen(temp) - lgout_del] = 0;
- printf("%s\n", temp);
- }
- free(del);
- free(out_del);
- free(text);
- free(column);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement