Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. /*write a program which copies its input to output and replaces
  2. each string of multiple blanks by one blank*/
  3.  
  4. #include <stdio.h>
  5. #define IN 1
  6. #define OUT 0
  7.  
  8. int main ()
  9. {
  10. int c, b, z; //c searches the chars, b goes IN on the first space,
  11. //z will be used to make it work for more than one space
  12.  
  13. b = OUT;
  14. z = OUT; //beginning of the loop, both are set to zero
  15. while ((c = getchar()) != EOF) //we search all the chars
  16. if (c == ' ')
  17. ++b; //If c picks up a space we set b to IN, starting the first loop
  18.  
  19. while (b == IN) //first loop
  20. {
  21.  
  22. if (c == ' ') //if the char after the first space is another space we
  23. // start the second loop by setting z to IN
  24. {
  25. b = OUT;
  26. z = IN;
  27. }
  28.  
  29. if (z == IN) //second loop
  30. {
  31. if (c == ' ') //if we find another space in the second loop we
  32. b = IN; //start the first loop again
  33. }
  34.  
  35. else printf("%c, c"); //if we dont find a space we print the char
  36.  
  37. printf("%c, c"); //I guess this prints all the chars that are not spaces??
  38.  
  39. }
  40. return(0);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement