Advertisement
Guest User

datoteke

a guest
Nov 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define maxi 50
  5.  
  6. int prebrojReci(char *);
  7. //int nadjiNajduzu(char *);
  8. int nadjiNajduzu2(char *);
  9.  
  10. int main(){
  11.  
  12.     FILE *ulaz,*izlaz;
  13.     char s[maxi];
  14.     int broj=0,max,k=0;
  15.     ulaz=fopen("ulaz.txt","r");
  16.     izlaz=fopen("izlaz.txt","w");
  17.     if(ulaz==NULL)
  18.     {
  19.         printf("nije moguce otvoriti datoteku!!\n");
  20.         return 1;
  21.     }
  22.     while(fgets(s,maxi,ulaz))
  23.     {
  24.         broj+=prebrojReci(s);
  25.         if(k==0)
  26.             {
  27.                 max=nadjiNajduzu2(s);
  28.                 k=1;
  29.             }
  30.         else
  31.             {
  32.                 if(nadjiNajduzu2(s)>max) max=nadjiNajduzu2(s);
  33.             }  
  34.     }
  35.     fprintf(izlaz,"od ukupno %d reci, najduza ima %d slova.\n",broj,max);
  36.     fclose(ulaz);
  37.     fclose(izlaz);
  38.     return 0;
  39. }
  40.  
  41. int prebrojReci(char *s)
  42. {
  43.     int broj=0,i=0;
  44.     while(s[i]!='\0')
  45.     {
  46.         if(s[i]==' ')broj++;
  47.         i++;
  48.     }
  49.     return broj+1;
  50. }
  51.  
  52. /*int nadjiNajduzu(char *s)
  53. {  
  54.  
  55.     int max=0,i=0,br=0,k=0;
  56.     while(s[i]!='\0')
  57.     {
  58.         if(k==0)
  59.             {
  60.                 if(s[i]==' ')
  61.                 {
  62.                     max=br;
  63.                     k=1;
  64.                     br=0;
  65.                 }
  66.                 br++;
  67.             }
  68.         else
  69.         {
  70.             br++;
  71.             if(s[i]==' '||s[i+1]=='\0')
  72.             {
  73.                 if(br>max)max=br;
  74.                 br=0;
  75.             }
  76.                
  77.         }  
  78.         i++;   
  79.     }
  80.     return max-1;
  81. }*/
  82.  
  83. int nadjiNajduzu2(char *s)
  84. {
  85.     char b[10]="";
  86.     int max,i,j=0,k=0;
  87.     for(i=0;i<=strlen(s);i++)
  88.     {
  89.         if(k==0)
  90.         {  
  91.             if(s[i]==' ')
  92.             {
  93.                 max=strlen(b);
  94.                 k=1;
  95.                 b[0]='\0';
  96.                 j=0;
  97.             }
  98.             b[j++]=s[i];
  99.         }
  100.         else
  101.         {
  102.             b[j++]=s[i];
  103.             if(s[i]==' ' || i==strlen(s))
  104.             {
  105.                 if(strlen(b)>max)max=strlen(b);
  106.                 b[0]='\0';
  107.                 j=0;
  108.             }
  109.         }
  110.  
  111.     }
  112.     return max;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement