visoft

Sortare de cuvinte din fisier

Nov 8th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. int cmpfunc (const void * a, const void * b) {
  7.     char* c1;
  8.     char* c2;
  9.     c1 = *(char**)a;
  10.     c2 = *(char**)b;
  11.     int rez=0;
  12.     rez = strcmp(c1, c2);
  13.     printf("%s %d %s\n", c1, rez, c2);
  14.     return rez;
  15. }
  16.  
  17. int main(){
  18.     FILE *f;
  19.     char str[1000] = {0};
  20. //  char str_test[] = "zeta theta eta epsilon alpha";
  21.     char* str_test;
  22.     f = fopen("src/in.txt", "r");
  23.     if (f == NULL){
  24.             printf("Could not open file %s",f);
  25.             return -1;
  26.     }
  27.  
  28.     int no_read_characters;
  29.     no_read_characters = fread(str, sizeof(char), 999, f);
  30.     printf("am citit %d: %s\n", no_read_characters, str);
  31.  
  32.     str_test = str;
  33.  
  34.     char* cuvinte[100];
  35.     int nr_cuvinte = 0;
  36.     char * token;
  37.     char* delimitatori = " \n?.!";
  38.     /* get the first token */
  39.     token = strtok(str_test, delimitatori);
  40.     /* walk through other tokens */
  41.     while( token != NULL ) {
  42. //      printf( "%s\n", token );
  43.         cuvinte[nr_cuvinte] = token;
  44.         nr_cuvinte++;
  45.         token = strtok(NULL, delimitatori);
  46.     }
  47.  
  48.     //  int start, stop, crt;
  49. //  crt = 0;
  50. //  while(str_test[crt] != 0){
  51. //      start = crt;
  52. //      while(('a' <= str_test[crt]) && (str_test[crt] <= 'z')){
  53. //          crt++;
  54. //      }
  55. //      stop = crt;
  56. //      printf("cuvantul tine de la %d la %d\n", start, stop);
  57. //      cuvinte[nr_cuvinte] = str_test + start;
  58. //      nr_cuvinte++;
  59. //      str_test[stop] = 0;
  60. //      printf("primul cuvant este *%s*\n", cuvinte[nr_cuvinte-1]);
  61. //      crt++;
  62. //  }
  63.  
  64. //  for(int i=0; i < nr_cuvinte; i++){
  65. //      for(int j = 0; j < nr_cuvinte - 1; j++){
  66. //          char* c1;
  67. //          char* c2;
  68. //          c1 = cuvinte[j];
  69. //          c2 = cuvinte[j + 1];
  70. //          int rez;
  71. //          rez = strcmp(c1, c2);
  72. ////            printf("%s %d %s\n", c1, rez, c2);
  73. //          if (rez > 0){
  74. //              cuvinte[j] = c2;
  75. //              cuvinte[j + 1] = c1;
  76. //          }
  77. //      }
  78. //  }
  79.     printf("Nr cuvinte: %d\n", nr_cuvinte);
  80.  
  81.     qsort(cuvinte, nr_cuvinte, sizeof(char*), cmpfunc);
  82.  
  83.     printf("Cuvintele sunt: ");
  84.     for(int i=0; i < nr_cuvinte; i++){
  85.         printf("%s ", cuvinte[i]);
  86.     }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. //  f = fopen("src/in.txt", "r");
  93. //  if (f == NULL){
  94. //          printf("Could not open file %s",f);
  95. //          return -1;
  96. //  }
  97. //
  98. //  int no_read_characters;
  99. //  no_read_characters = fread(str, sizeof(char), 999, f);
  100. //  printf("am citit %d: %s\n", no_read_characters, str);
  101. //
  102. //
  103.     fclose(f);
  104.     printf("\ndone");
  105. }
Add Comment
Please, Sign In to add comment