visoft

Pregatire 19 Nov Numarare non white characters

Nov 19th, 2020
469
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_LINE_LEN 10000
  6. #define MAX_NO_LINES 100
  7.  
  8. /*
  9.  * On success, the function returns str.
  10. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof).
  11.  If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
  12. If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).
  13.  *
  14.  */
  15.  
  16. /*
  17.  * void* malloc (size_t size);
  18.  *
  19.  *
  20.  */
  21.  
  22. int main() {
  23.     FILE * f = fopen("../in.txt", "r");
  24.  
  25.     char **tab = (char **)malloc(sizeof(char*)*MAX_NO_LINES);
  26.  
  27.     if (f == NULL)
  28.         return 100;
  29.  
  30.     char* linie = (char *)malloc(sizeof(char)* MAX_LINE_LEN);
  31.     char* ok=linie;
  32.     int n = 0;
  33.     while (ok == linie){
  34.         ok = fgets(linie, MAX_LINE_LEN, f);
  35.         if(ok != linie)
  36.             break;
  37.         tab[n] = (char*)malloc(sizeof(char) * (strlen(linie) + 1));
  38.         strcpy(tab[n], linie);
  39.         n++;
  40.     }
  41.     free(linie);
  42.     fclose(f);
  43.  
  44.     //**************************************************  AICI AM CONTINUTUL IN MEMORIE ************//
  45.  
  46.     for(int i=0;i<n;i++){
  47.         //Sa numaram spatiile
  48.         int nr_spatii = 0;
  49.         for(int j = 0; j < strlen(tab[i]);j++){
  50.             int is_whitespace = tab[i][j] == ' ' || tab[i][j] == '\n' || tab[i][j] == '\r';
  51.             if(!is_whitespace)
  52.                 nr_spatii++;
  53.         }
  54.         printf("Numar spatii: %d\n", nr_spatii);
  55.     }
  56.  
  57.  
  58.  
  59.     for(int i=0;i<n;i++){
  60.         free(tab[i]);
  61.     }
  62.     free(tab);
  63.  
  64.     return 0;
  65. }
  66.  
  67.  
RAW Paste Data