Advertisement
gavrilo93

broj reci zbrda i zdola

Nov 15th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* broj reci je broj razmaka plus jedan
  4. while se ucitava broje se slova, kada naidje na razmak broj slova se upisuje u neki max slova
  5. do while pisi na neki fajl slova dok se ne dodje do razmaka*/
  6.  
  7. int brojacReci(){
  8.     FILE *f1 = fopen("test.txt", "r");
  9.     char c;
  10.     int brReci = 1;
  11.     while (fscanf(f1, "%c", &c) !=EOF)
  12.     {
  13.         if (c == ' ')
  14.             brReci++;
  15.     }
  16.  
  17.     fclose(f1);
  18.     return brReci;
  19.  
  20.  
  21.  
  22. }
  23. void ispisNajReci(int n){
  24.  
  25.      int x = 0;
  26.      char c;
  27.     FILE *f2 = fopen("ispis.txt", "w");
  28.     FILE *f1 = fopen("test.txt", "r");
  29.  
  30.     while (fscanf(f1, "%c", &c) !=EOF) {
  31.     if (c==' '){
  32.      x++; printf("%d\n", x);}
  33.  
  34.     if (x==2){
  35.         fprintf(f2, "%c", c);}
  36.  
  37.         }
  38.     fclose(f2);
  39.     fclose(f1);
  40.  
  41. }
  42.  
  43. int main (){
  44.  FILE *f1 = fopen("test.txt", "r");
  45.  
  46.  
  47.  
  48.  char c;
  49.  int k = brojacReci();
  50.  int brSlova = 0;
  51.  int maxBr = 0;
  52.  int najRec = 0;
  53.  int num = 0;
  54.  int naj = 0;
  55.  while (fscanf(f1, "%c", &c) !=EOF) {
  56.      brSlova++;
  57.  
  58.      if (c == ' '){
  59.          num++;
  60.  
  61.  
  62.             if ((brSlova-1)> maxBr){
  63.                 naj = num;
  64.             maxBr = brSlova-1;}
  65.  
  66.  
  67.             brSlova = 0;}
  68.         }
  69.     ispisNajReci(naj);
  70.  
  71.         printf("broj reci je: %d\t broj max slova je: %d\t najduza rec je : %d", k, maxBr, naj);
  72. fclose(f1);
  73.  
  74.     return 0;
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement