Guest User

Untitled

a guest
Aug 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1.  #include <stdio.h>
  2.     #include <string.h>
  3.     void counter(char *str)
  4.     {
  5.         int i,l;
  6.  
  7.         l=1;
  8.         for(i=0;(str[i]!=0);i++)
  9.         {
  10.             if ((str[i-1]!=' ')&&(str[i]==' '))
  11.             {
  12.                 if (i!=0) l++;//считаем количество слов в строке
  13.             }
  14.         }
  15.         printf("\nWord count in string = %d\n",l);
  16.  
  17.         if(l%2 == 0) //если в строке четное количество слов то удаляем лишнее
  18.         {
  19.             vivod(str);//вот этой функцией
  20.         }//если не четное то ничего не меняем, оставляем как было
  21.     }
  22.  
  23.  
  24.  
  25.     void vivod(char *str)
  26.     {
  27.         int j,i,l;
  28.  
  29.         l=1;
  30.         char str1[200]="0";
  31.         for(j=0,i=0;(str[i]!=0);i++)
  32.         {
  33.             if ((str[i-1]!=' ')&&(str[i]==' '))
  34.             {
  35.                 if (i!=0) l++;
  36.             }
  37.             if ((l%2!=0)||(l%2==0)&&(str[i]==' '))
  38.             {
  39.                 str1[j]=str[i]; j++;
  40.             };
  41.         }
  42.         strcpy(str,str1);
  43.     }
  44.  
  45.     main()
  46.     {
  47.         FILE *in_file,*out_file;
  48.             char file_name[200]="0";//имя входного файла
  49.             char a[200]="0";
  50.             char *ptr;
  51.             printf("\n Enter file name:");
  52.             gets(file_name);
  53.             in_file=fopen(file_name,"rt");
  54.             if(in_file==NULL)
  55.             {
  56.                     printf("Error open file %s",file_name);
  57.                     return 1;
  58.             }
  59.  
  60.             out_file=fopen("result.txt","wt");
  61.             if(out_file==NULL)
  62.             {
  63.                     printf("Eror create file %s",file_name);
  64.                     return 1;
  65.             }
  66.             //int k=0;
  67.  
  68.             while(feof(in_file)==0)
  69.                if(fgets(a,200,in_file)!=NULL)
  70.                {
  71.                             ptr=a;
  72.                 counter(ptr);
  73.                 a[strlen(a)-1]=0;
  74.                 printf("%s\n",a);
  75.                 fprintf(out_file,"%s\n",a);
  76.  
  77.                     }
  78.             fclose(in_file);
  79.             fclose(out_file);
  80.             printf("\n Result into <result.txt>");
  81.     }
Add Comment
Please, Sign In to add comment