one word per line


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /* writes one word per line*/
  2.  
  3. #include <stdio.h>
  4.  
  5. #define YES 1
  6. #define NO 0
  7.  
  8. int main (int argc, const char * argv[]){
  9. int c, nl, INWORD;
  10. INWORD = NO;
  11. nl='\n';
  12. while((c=getchar())!=EOF){
  13. if(INWORD==YES){
  14. if (c=='\n' || c=='\t' || c==' ') {
  15. putchar(nl);
  16. INWORD=NO;
  17. c=getchar();
  18. }
  19. else {
  20. putchar(c);
  21. c=getchar();
  22. }
  23. }
  24. else {
  25. if (c=='\n' || c=='\t' || c==' ') {
  26. c=getchar();
  27. }
  28. else{
  29. INWORD=YES;
  30. putchar(c);
  31. c=getchar();
  32. }
  33. }
  34. }
  35. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.