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 str[1000] = {0};
- // char str_test[] = "zeta theta eta epsilon alpha";
- char* str_test;
- f = fopen("src/in.txt", "r");
- if (f == NULL){
- printf("Could not open file %s",f);
- return -1;
- }
- int no_read_characters;
- no_read_characters = fread(str, sizeof(char), 999, f);
- printf("am citit %d: %s\n", no_read_characters, str);
- str_test = str;
- char* cuvinte[100];
- int nr_cuvinte = 0;
- char * token;
- char* delimitatori = " \n?.!";
- /* get the first token */
- token = strtok(str_test, delimitatori);
- /* walk through other tokens */
- while( token != NULL ) {
- // printf( "%s\n", token );
- cuvinte[nr_cuvinte] = token;
- nr_cuvinte++;
- token = strtok(NULL, delimitatori);
- }
- // int start, stop, crt;
- // crt = 0;
- // while(str_test[crt] != 0){
- // start = crt;
- // while(('a' <= str_test[crt]) && (str_test[crt] <= 'z')){
- // crt++;
- // }
- // stop = crt;
- // printf("cuvantul tine de la %d la %d\n", start, stop);
- // cuvinte[nr_cuvinte] = str_test + start;
- // nr_cuvinte++;
- // str_test[stop] = 0;
- // printf("primul cuvant este *%s*\n", cuvinte[nr_cuvinte-1]);
- // crt++;
- // }
- // for(int i=0; i < nr_cuvinte; i++){
- // for(int j = 0; j < nr_cuvinte - 1; j++){
- // char* c1;
- // char* c2;
- // c1 = cuvinte[j];
- // c2 = cuvinte[j + 1];
- // int rez;
- // rez = strcmp(c1, c2);
- //// printf("%s %d %s\n", c1, rez, c2);
- // if (rez > 0){
- // cuvinte[j] = c2;
- // cuvinte[j + 1] = c1;
- // }
- // }
- // }
- 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]);
- }
- // f = fopen("src/in.txt", "r");
- // if (f == NULL){
- // printf("Could not open file %s",f);
- // return -1;
- // }
- //
- // int no_read_characters;
- // no_read_characters = fread(str, sizeof(char), 999, f);
- // printf("am citit %d: %s\n", no_read_characters, str);
- //
- //
- fclose(f);
- printf("\ndone");
- }
Add Comment
Please, Sign In to add comment