home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12316 < prev    next >
Encoding:
Text File  |  1992-08-13  |  2.2 KB  |  88 lines

  1. Xref: sparky comp.lang.c:12316 comp.programming:2309
  2. Newsgroups: comp.lang.c,comp.programming
  3. Path: sparky!uunet!usc!sdd.hp.com!caen!destroyer!ubc-cs!alberta!cpsc.ucalgary.ca!jade!west
  4. From: west@jade.ab.ca (Darrin West)
  5. Subject: Re: A LITTLE BRAINTEASER...
  6. Message-ID: <1992Aug13.141021.16870@jade.ab.ca>
  7. Organization: Jade Simulations International, Inc.
  8. References: <aet.713608023@munagin>
  9. Date: Thu, 13 Aug 1992 14:10:21 GMT
  10. Lines: 76
  11.  
  12. This solution does use a struct and a linked list (so I guess it HAS
  13. data structures), but it has the advantage (:-) of using only one
  14. function and no loops, and certainly does not use the heap.
  15.  
  16. #include <stdio.h>
  17. struct X {
  18.     char ch;
  19.     struct X *last;
  20. };
  21.  
  22. /* this program only works if called with no arguments */
  23. main(argc)
  24. struct X *argc;
  25. {
  26.     struct X x;
  27.  
  28.     x.ch=getchar();
  29.     x.last=argc;
  30.  
  31.     if (x.ch == EOF){
  32.     /* we are now in the printing phase */
  33.     if (argc != (struct X*)1) {
  34.         /* we are in the middle of a line */
  35.         main(argc->last);  /* print the first of the line */
  36.         putchar(argc->ch); /* and then print this letter */
  37.     }
  38.     /* we reached the front of the line */
  39.     }
  40.     else{
  41.     /* we are in the reading phase */
  42.     if (x.ch == '\n') main(1); /* keep reading; mark the end of the line */
  43.  
  44.     /* this is the neat part, in most cases this reads the rest of the
  45.        line but when the previous statement was executed (we are at the end
  46.        of a line) this prints the current line. */
  47.     main(&x);
  48.     }
  49. }
  50.  
  51. I had considered not using a struct, but just two variables juxtaposed on
  52. the stack and doing some pointer incrementing, but that isn't portable :-).
  53. (maybe if I did this: int *last1;char ch;int *last2; and set both last
  54.  pointers, it would work on inverted stack machines too.)
  55.  
  56. Ok, I tried it (looks even better, and might even port! :-)
  57.  
  58. #include <stdio.h>
  59.  
  60. main(argc)
  61. int *argc;
  62. {
  63.     int *last1;
  64.     int ch;
  65.     int *last2;
  66.  
  67.     ch = getchar();
  68.     last1 = last2 = argc;
  69.  
  70.     if (ch == EOF){
  71.     if (argc != (int*)1) {
  72.         main(*(argc+1));
  73.         putchar(*argc);
  74.     }
  75.     }
  76.     else{
  77.     if (ch == '\n') main(1);
  78.     main(&ch);
  79.     }
  80. }
  81.  
  82.  
  83. Anyone for a grosser solution?
  84.  
  85. -- 
  86. Darrin West, MSc.
  87.   Jade Simulations International Corporation. west@jade.ab.ca
  88.