Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int cmpfunc (const void * a, const void * b) {
- char* c1;
- char* c2;
- c1 = *(char**)a;
- c2 = *(char**)b;
- int rez=0;
- rez = strcmp(c1, c2);
- printf("%s %d %s\n", c1, rez, c2);
- return rez;
- }
- int main(){
- FILE *f;
- char* nume_fisier = "src/in.txt";
- char* str;
- f = fopen(nume_fisier, "r");
- if (f == NULL){
- printf("Could not open file %s",nume_fisier);
- return -1;
- }
- fseek(f, 0L, SEEK_END);
- int file_length = ftell(f);
- fseek(f, 0L, SEEK_SET);
- str = (char*)malloc((file_length +1) * sizeof(char));
- memset(str, 0, (file_length +1) * sizeof(char));
- int no_read_characters;
- no_read_characters = fread(str, sizeof(char), file_length, f);
- printf("am citit %d caractere\n%s\n", no_read_characters, str);
- char* cuvinte[100];
- int nr_cuvinte = 0;
- char * token;
- char* delimitatori = " \n?.!";
- /* get the first token */
- token = strtok(str, delimitatori);
- /* walk through other tokens */
- while( token != NULL ) {
- cuvinte[nr_cuvinte] = token;
- nr_cuvinte++;
- token = strtok(NULL, delimitatori);
- }
- printf("Nr cuvinte: %d\n", nr_cuvinte);
- qsort(cuvinte, nr_cuvinte, sizeof(char*), cmpfunc);
- printf("Cuvintele sunt: ");
- for(int i=0; i < nr_cuvinte; i++){
- printf("%s ", cuvinte[i]);
- }
- fclose(f);
- free(str);
- printf("\ndone");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement