Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- int peek();
- int main()
- {
- int nrlines=0,nrwords=0;
- int c;
- while((c=getchar())!=EOF)
- {
- if(!isspace(c))
- for(++nrwords;!isspace(c=getchar());)
- if(c==EOF)
- {
- nrlines++;
- break;
- }
- if(c=='\n')
- {
- nrlines++;
- if(peek()=='\n')
- {
- printf("the number of lines is %d and the number of words is %d\n",nrlines,nrwords);
- nrlines=0;
- nrwords=0;
- }
- getchar(); // consuming the last \n from above
- }
- }
- return 0;
- }
- int peek()
- {
- return ungetc(getchar(),stdin);
- }
- PROBLEMA 2 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- /*Collapse spaces Write a program that reads all input
- it transformed as follows:
- * any sequence of whitespace that does not contain newline
- is replaced with a single space character
- * whitespace characters immediately before a newline are deleted
- */
- int peek()
- {
- return ungetc(getchar(),stdin);
- }
- void spaceerase()
- {
- while(peek()==' ')
- {
- getchar();
- }
- if(peek()!='\n')
- {
- putchar(' ');
- }
- }
- int main()
- {
- int c;
- while((c=getchar())!=EOF)
- {
- if(isspace(c))
- {
- spaceerase();
- }
- if(!isspace(c))
- putchar(c);
- }
- return 0;
- }
- PROBLEMA 1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- int peek();
- void width(unsigned n);
- int main()
- {
- width(5);
- return 0;
- }
- void width(unsigned n)
- {
- int c,nrl=0;
- while((c=getchar())!=EOF)
- {
- nrl++;
- if(nrl==n)
- {
- if(c==' ')
- {
- putchar(c);
- putchar('\n');
- }else{
- putchar(c);
- putchar('-');
- putchar('\n');
- }
- nrl=0;
- }else{
- putchar(c);
- }
- }
- }
- int peek()
- {
- return ungetc(getchar(),stdin);
- }
- PROBLEMA 3 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- /* 4. Prettyprinting Write a program that properly
- indents a text read from standard input that has balanced
- braces { } . The output is formed as follows:
- Any brace must be followed by a newline.
- Any non-empty line starts with 2n spaces, where n is the
- number of braces opened and not yet closed. After these
- spaces, a non-whitespace character must follow.
- No other changes are done to the text. */
- #include <stdio.h>
- #include <ctype.h>
- int peek()
- {
- return ungetc(getchar(), stdin);
- }
- void spaces(int b)
- {
- for(int i = 1; i <= 2*b; i++) // print 2n spaces
- putchar(' ');
- while(isspace(peek())) // after n spaces, we need a non-whitespace character
- getchar(); // so we consume all whitespace characters
- }
- void main()
- {
- unsigned space = 0;
- int c; // bracket level so we know how many spaces to print
- while((c=getchar())!=EOF)
- {
- if(c=='{' || c=='}')
- {
- if(c=='{'){
- space++;
- }else{
- space--;
- }
- putchar(c);
- putchar('\n');
- spaces(space);
- }
- if(c=='\n')
- {
- putchar(c);
- spaces(space);
- }
- if(c!='\n' && c!='{' && c!='}')
- {
- putchar(c);
- }
- }
- }
- PROBLEMA 4 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- #include <stdio.h>
- #include <ctype.h>
- int peek(void);
- int main()
- {
- int c;
- int contor=0;
- while((c=getchar())!=EOF)
- {
- if(c=='(')
- {
- if(peek()=='*')
- {
- contor++;
- }
- }else
- if(c=='*')
- {
- if(peek()==')')
- {
- contor--;
- getchar();
- }
- }else
- if(contor==0){
- putchar(c);
- }
- }
- }
- int peek(void)
- {
- return ungetc(getchar(), stdin);
- }
- PROBLEMA 5 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement