"C the Programming Language" exercise 1-9

Member
Posts: 63
Joined: 2005.04
Post: #1
Is there a cleaner more compact way of doing this

Code:
#include <stdio.h>

// Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank

#define INABLANK    a
#define CURRENTCHAR c
#define FALSE 0
#define TRUE 1



main(){

int CURRENTCHAR = 0;
int INABLANK = FALSE;


    while((CURRENTCHAR = getchar()) != EOF){
                                                        
                            if(INABLANK == TRUE && CURRENTCHAR == ' '){
                                        ;
                                    }
                                
                            else
                            if(INABLANK == FALSE && CURRENTCHAR == ' '){
                                    putchar(CURRENTCHAR);
                                    INABLANK = TRUE;
                            }
                    

                            else{
                            putchar(CURRENTCHAR);
                            INABLANK = FALSE;
                            }
    }
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
why the heck are you using #defines as variable names Blink
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #3
Code:
sed -E 's/ +/ /g'
might do the trick

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
PHP Code:
#include <stdio.h>

void main()
{
    
char c;
    
int previousBlank 0;
    while ((
getchar()) != EOF) {
        if (
== ' ') {
            if (!
previousBlank) {
                
putchar(' ');
                
previousBlank 1;
            }
        }
        else {
            
putchar(c);
            
previousBlank 0;
        }
    }

Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #5
OneSadCookie Wrote:why the heck are you using #defines as variable names Blink
He must be Russian. The only other person I know who does this is also Russian. They are a strange breed. Then again this other bloke does stuff like this...

Code:
struct {
   int dolt   ...
} Dolty;
...
Dolty increadibleGlobal;
...
#define WHATAMI increadibleGlobal.dolt
...
   WHATAMI = BLOODYHELL;

So when you get down to the WHATAMI=... the debugger has a fit in that it can't show you what it is or it's value. You have to hunt the code for it's meaning and when you do, you completely forget the context on the chunk you are trying to debug. Programmers (actually anyone who does this should remove programmer from their description) who do this are idiots.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #6
"He must be Russian."
I'm from Canada, they say I'm slow eh

"why the heck are you using #defines as variable names "

lol I don't know, I am at very beginning stages of learning C. I find I make mistakes in consistently odd ways.

This program works as it should for everything but backspaces, why?...

Code:
// Exercise 1-10

#include <stdio.h>

main(){

int currentchar = 0;

while((currentchar = getchar()) != EOF){
if(currentchar == '\t'){
    putchar('\\');
    putchar('t'); }
else
if(currentchar == '\b'){
    putchar('\\');
    putchar('b');}
else
if(currentchar == '/'){
    putchar('/');
    putchar('/');}
else
    putchar(currentchar);
}
}
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #7
Just starting! Oh well then, no worries. Wink

getchar() probably doesn't work how you want it to work. Or at least on a windows box, which I am on at the moment, I have to hit enter for getchar() to return control back to the loop. Replacing getchar() with something like _getch() which will return a key value once you type something at the keyboard will work as you expect. (Or at least on the windows box. I don't know what the exact unix function would be called as _getch(), _getche() and _kbhit() are system dependent routines found in conio.h. Haven't done a lot of console stuff on the mac to know if this exists there as well.)

A bit of a search at the apple site ther is a getch() in curses.h which will probably do what you are looking for. But curses.h from my cobwebbed head is for console display manipulation. I.E. for old school text based applications. In other words, there might be more involved when using it. I've never used it either.

http://developer.apple.com/documentation...ch.3x.html
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #8
Quote:Programmers (actually anyone who does this should remove programmer from their description) who do this are idiots.
I disagree, programmers who do this are probably not writing code for legability. I imagine he might have been coding for hours up really late and did somthing like this as a joke to himself, and probably being an expert programmer doesn't see this as confusing.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #9
unknown Wrote:I disagree, programmers who do this are probably not writing code for legability. I imagine he might have been coding for hours up really late and did somthing like this as a joke to himself, and probably being an expert programmer doesn't see this as confusing.
The only reason to use macroes or #defines is to improve legibility. However if it leads to other problems like causing grief for debeugging purposes then it is worthless. Plus, who writes code so that it isn't legible. That is wrong on so many levels. If you want to obfuscate then there are plenty of tools out there that do just that. No need to make it hard on yourself to begin with.

But personally, I stand by my opinion. People who do that are idiots and I curse their name every time I have to maintain their code.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #10
Zekaric Wrote:The only reason to use macroes or #defines is to improve legibility.

Goodness, no. #define exists so that you can assign a value to a constant in a single place and use it throughout your program. If you want to change the value of the constant (or the behavior of the macro), you can change it in a single centralized location, instead of having to find and replace every occurrence throughout your application. Improving legibility is secondary.

- Alex Diener
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #11
Quote:Goodness, no. #define exists so that you can assign a value to a constant in a single place and use it throughout your program. If you want to change the value of the constant (or the behavior of the macro), you can change it in a single centralized location, instead of having to find and replace every occurrence throughout your application. Improving legibility is secondary.

Yes that is true as well but, based on point of view, I see that as an offshoot of why you used a macro/#define. Why? Case...
Code:
switch (flag) {
case 0:
...
case 10:
...
case 42:
...
}
The above is rubbish code. What do 0, 10 and 42 mean? Improved version.
Code:
/* #defines or an enum, tomato tomatoe in this instance.*/
typedef enum {
   flagADD_NEW,
   flagPROCESS,
   flagDELETE_ME
} Flag;
...
switch (flag) {
case flagADD_NEW:
...
case flagPROCESS:
...
case flagDELETE_ME:
...
}
No guesswork. Even in this case...
Code:
value = var1 * 2.718281828;
Is less clear than.
Code:
value = var1 * NATURAL_NUMBER_E;
How many knew that number I was multiplying with was e off the bat? This may only ever be used once in a source file but I'd use a #define... (Or at least a comment about that number. Wink But that's another thread which has reached quite a length.)

My view of the world, you program for legibility first. Code that is easy to read is easy to understand. Remove the guesswork where ever you can. So in my world, #defines are there to improve legibility; not as much for the reason you pointed out but that is also a very valid reason...

Erm... This is slowing turning into that other thread about commenting and we're getting clearly off topic here.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #12
Your backspace problem may have something to do with the fact that backspaces are rarely stored in text - usually it just deletes a character instead. What are you reading from?

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #13
Perhaps the question I'm on is a trick one.

from the book, page 20 : "Exercise 1-10 Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #14
I think most modern terminal emulators keep a line buffer and instead of inserting backspaces they delete a character from the buffer before it's sent to the program... in which case you can probably ignore that part of the exercise Smile

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #15
Byron Clarke Wrote:This program works as it should for everything but backspaces, why?..

Zekaric Wrote:getchar() probably doesn't work how you want it to work. Or at least on a windows box, which I am on at the moment, I have to hit enter for getchar() to return control back to the loop. Replacing getchar() with something like _getch() which will return a key value once you type something at the keyboard will work as you expect. (Or at least on the windows box. I don't know what the exact unix function would be called as _getch(), _getche() and _kbhit() are system dependent routines found in conio.h. Haven't done a lot of console stuff on the mac to know if this exists there as well.)

The problem is that, from I can gather from the description of every exercise, this little programs are not for interactive use.

Try this:

1. Take the code I posted above (be sure to fix the spaces/weird chars that may appear if you copy it from the webpage).
2. Paste it in a text file, call it "noextraspaces.c", and save it in the Desktop.
3. Create another text file, type some stuff in it, including a few runs of spaces, call it test.txt and save it to the Desktop as well.
4. Open the Terminal and type:
Code:
cd Desktop
gcc noextraspaces.c -o noextraspaces.o
./noextraspaces.o < test.txt

The last line will run your compiled program, giving it the text file as its input. The output will be that file, with extra spaces removed.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  which programming language is best? petr6534 27 11,550 Sep 30, 2004 10:00 AM
Last Post: PowerMacX