Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. int peek();
  6. int main()
  7. {
  8.     int nrlines=0,nrwords=0;
  9.     int c;
  10.     while((c=getchar())!=EOF)
  11.     {
  12.         if(!isspace(c))
  13.         for(++nrwords;!isspace(c=getchar());)
  14.         if(c==EOF)
  15.         {
  16.             nrlines++;
  17.             break;
  18.         }
  19.         if(c=='\n')
  20.         {
  21.             nrlines++;
  22.             if(peek()=='\n')
  23.             {
  24.                 printf("the number of lines is %d and the number of words is %d\n",nrlines,nrwords);
  25.                 nrlines=0;
  26.                 nrwords=0;
  27.             }
  28.             getchar(); // consuming the last \n from above
  29.         }
  30.     }
  31.     return 0;
  32. }
  33. int peek()
  34. {
  35.     return ungetc(getchar(),stdin);
  36. }
  37.  
  38.  
  39. PROBLEMA 2 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <ctype.h>
  43. /*Collapse spaces Write a program that reads all input
  44. it transformed as follows:
  45. * any sequence of whitespace that does not contain newline
  46. is replaced with a single space character
  47. * whitespace characters immediately before a newline are deleted
  48. */
  49. int peek()
  50. {
  51.     return ungetc(getchar(),stdin);
  52. }
  53. void spaceerase()
  54. {
  55.     while(peek()==' ')
  56.         {
  57.             getchar();
  58.         }
  59.     if(peek()!='\n')
  60.     {
  61.         putchar(' ');
  62.     }
  63. }
  64. int main()
  65. {
  66.     int c;
  67.     while((c=getchar())!=EOF)
  68.     {
  69.         if(isspace(c))
  70.         {
  71.             spaceerase();
  72.         }
  73.         if(!isspace(c))
  74.         putchar(c);
  75.     }
  76.     return 0;
  77. }
  78.  
  79. PROBLEMA 1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include <ctype.h>
  83. int peek();
  84. void width(unsigned n);
  85. int main()
  86. {
  87.     width(5);
  88.     return 0;
  89. }
  90. void width(unsigned n)
  91. {
  92.     int c,nrl=0;
  93.     while((c=getchar())!=EOF)
  94.     {
  95.      nrl++;
  96.      if(nrl==n)
  97.      {
  98.          if(c==' ')
  99.          {
  100.              putchar(c);
  101.              putchar('\n');
  102.          }else{
  103.              putchar(c);
  104.              putchar('-');
  105.              putchar('\n');
  106.          }
  107.          nrl=0;
  108.      }else{
  109.      putchar(c);
  110.      }
  111.     }
  112. }
  113. int peek()
  114. {
  115.     return ungetc(getchar(),stdin);
  116. }
  117.  
  118. PROBLEMA 3 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  119.  
  120. /* 4. Prettyprinting Write a program that properly
  121. indents a text read from standard input that has balanced
  122. braces { } . The output is formed as follows:
  123.  
  124. Any brace must be followed by a newline.
  125. Any non-empty line starts with 2n spaces, where n is the
  126. number of braces opened and not yet closed. After these
  127. spaces, a non-whitespace character must follow.
  128. No other changes are done to the text. */
  129.  
  130. #include <stdio.h>
  131. #include <ctype.h>
  132.  
  133. int peek()
  134. {
  135.       return ungetc(getchar(), stdin);
  136. }
  137.  
  138. void spaces(int b)
  139. {
  140.       for(int i = 1; i <= 2*b; i++)                      // print 2n spaces
  141.             putchar(' ');
  142.       while(isspace(peek()))                        // after n spaces, we need a non-whitespace character
  143.             getchar();                                     // so we consume all whitespace characters
  144. }
  145.  
  146. void main()
  147. {
  148.       unsigned space = 0;
  149.       int c;                                      // bracket level so we know how many spaces to print
  150.       while((c=getchar())!=EOF)
  151.       {
  152.             if(c=='{' || c=='}')
  153.                 {
  154.                     if(c=='{'){
  155.                     space++;
  156.                     }else{
  157.                     space--;
  158.                     }
  159.                     putchar(c);
  160.                     putchar('\n');
  161.                     spaces(space);
  162.                 }
  163.             if(c=='\n')
  164.             {
  165.                 putchar(c);
  166.                 spaces(space);
  167.             }
  168.             if(c!='\n' && c!='{' && c!='}')
  169.                 {
  170.                     putchar(c);
  171.                 }
  172.       }
  173. }
  174.  
  175. PROBLEMA 4 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  176.  
  177. #include <stdio.h>
  178. #include <ctype.h>
  179.  
  180. int peek(void);
  181.  
  182. int main()
  183. {
  184.     int c;
  185.     int contor=0;
  186.     while((c=getchar())!=EOF)
  187.     {
  188.         if(c=='(')
  189.         {
  190.             if(peek()=='*')
  191.             {
  192.                 contor++;
  193.             }
  194.         }else
  195.         if(c=='*')
  196.         {
  197.             if(peek()==')')
  198.             {
  199.                 contor--;
  200.                 getchar();
  201.             }
  202.         }else
  203.         if(contor==0){
  204.             putchar(c);
  205.         }
  206.     }
  207. }
  208. int peek(void)
  209. {
  210.       return ungetc(getchar(), stdin);
  211. }
  212.  
  213. PROBLEMA 5 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement