C Beginner Looking For Suggestions

A few weeks ago at the recommendation of people I trust, I bought and started reading Kernighan and Ritchie's (K&R) C Programming Language. For one thing, it's damn thin compared to the O'Reilly Practical C I just finished last month. It covers generally the same stuff but in a much more efficient manner. Of course, what do I know? I'm only on chapter 1 and I'm taking K&R's advice and pausing my reading to work through the programming problems in the chapter. One thing that's kind of frustrating to me is that I ran into a problem that seems like it could have an elegant solution, but I've not come up with one. Problem 1-9 which says:

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

At first I tried coming up with my own code from scratch and wound up with some freakish binary that spit out unprintable characters (probably a mix up in data types). So I started over and entered their file copying program example (1.5.1) from page 16. Then I thought about how to modify it. Here is what I came up with:

#include <stdio.h>

int main()
{
  int c, space;

  space = 0;
  c = getchar();

  while (c != EOF) {
  if (c == 32 && space == 1)
    /* do nothing */;
  else if (c != ' ') {
    space = 0;
    putchar(c);}
  else if (c == ' ') {
    space = 1;
    putchar(c);}
    
    c = getchar();
  }
}

(NOTE: I used 32 for space in the first if conditional while testing and didn't switch it back to c == ' '.)

The compiled program works on the most basic level, but I'm sure it's bug ridden and the wrong input will make it barf. Since it does what it's supposed to, I've moved on. But I'm strangely bothered since I *KNOW* there has to be a better way than a bunch of serial if conditionals. I get this nagging feeling that I could do it shorter and with fewer checks.

But I just couldn't do it. Anyone else see where I'm totally off? This is definitely beginner style code and I admit as much. But I was pretty bad in the beginning with Bash too (I had a full page script in 2001 to generate MP3 playlists by traversing directories. Today I have a two liner using the 'find' command), so I think there is hope for my dream of one day being fluent at C. Anyone have any suggestions about my above code? Where could my thinking be improved?

I'll also say that I have a lot of trouble thinking in parallel, but it seems that that is how the best coders think. My code has always been serial in DOS BAT files, CMD, Bash, Perl and now C. Any tips on changing that way of seeing things? Also, let me know if this isn't the right place to post this.

NOTE: I know there's an "answer book" for this book. But I want to do it the "hard way" first and then when I'm done I'll compare my answers with the ones in the answer book.

Here is one way:

#include <stdio.h>
#define SPC 32

int main()
{
  int c=0;
  int space=0;
  space = 0;

  while ((c=getchar()) != EOF) 
  {
      space=(c==SPC) ? space+1 : 0;
      if(space < 2 ) 
         putchar(c);
  }
  
  return 0;
}

space=(c==SPC) ? space+1 : 0;

Is another way of writing if c equals space add one to space else move 0 to space.

#define SPC 32
Defining constants is good practice.

Bad: this code never checks return values of functions. It would fail lint.

hows this?

#include<stdio.h>
#define SP 32
#define BRK_LOP 16

int main()
{
    char ch;
    int flag=0;
    puts("Press ctrl+p to stop");
    while((ch=fgetc(stdin))!=BRK_LOP)
    {
        if(ch!=SP)
        {
            fputc(ch,stdout);
            flag=0;
        }
        else
        {
            if(flag==0)
               fputc(ch,stdout);
            flag=1;            
        } 

    }
    return 0;
}

parallel in what sense?

seeing jim mcnamara's code gave me an idea

#include<stdio.h>
#define SP 32
#define BRK_LOP 16

int main()
{
    char ch;
    int flag=0;
    puts("Press ctrl+p to stop");
    while((ch=fgetc(stdin))!=BRK_LOP)
       ch!=SP ? flag=fputc(ch,stdout)-ch : ( flag==0 ? flag=fputc(ch,stdout) : 0 );
    return 0;
}

now, hows this?

When I've seen him do PHP queries against a MySQL DB, he doesn't just do a single query. He'll usually pull in multiple bits of information even though to me it doesn't seem relevant. But afterwards in his PHP script, the data returned from the queries is used in groups. Kind of hard to explain I guess. It's just that my thinking would be:

Execute a query and pull in one column of data
Process the data
Execute another query for another column
Process that data
etc...
Then take all the data stored in arrays and format it for display

Whereas his thinking seems to be

Execute a query to pull in all needed data from multiple tables
Process in one function and format for display

I guess maybe it just takes having a lot of familiarity with the language and you start to see the quicker way of doing things. His approaches also seem to be able to avoid the spaghetti logic I get myself into sometimes by nature of their compactness.

Well, keep in mind that cryptic code which is not self-documenting, might seem elegant and advanced, but it can be a nightmare to maintain, especially by others who inherit the code later.

spaghetti comes from not knowing what logic elements get repeated or what one logic element actually controls program flow.

I should also mention -

  1. Initialize variables when you declare them.

  2. K & R is very old - the content is great, the code format is not modern at all.

  3. Build a function to do one thing well, not many things.

who is "he"?
if you described it to me, that means you too are thinking in that way...for describing it to me...:rolleyes::stuck_out_tongue:
dont push yourself too hard...

no its not the language really, its the familiarity with the concept that you apply...the technical details can always be found in the documentation, concepts cannot.

Wow! Thank you everyone! As always unix.com comes through with some good insight. It's quite encouraging and inspiring. So I will press onward. I especially appreciated the suggestion that understanding the concepts if more important than understanding the language. That's been my approach to learning GNU/Linux to the point where I can use most distros fairly easily. I just need to do that with programming and maybe the jump from Bash to C will be mirrored in C to C++, PHP, Perl, Javascript and Java. (It might sound ambitious but I really want to get there) Now back to K&R C (this is the second edition which covers C89 ANSI C).

I got a nice simple response from a friend on another forum. (I can't be sure he didn't cheat and just post the answer from the K&R answer book, but I'll give him the benefit of the doubt)

int main()
{
    c = getchar();
    while (c != EOF)
    {
        putchar(c);
        do
        {
            c = getchar();
        } while ( c != EOF && c == ' ');
    }
    return 0;
}

Although I'm not yet used to the do-while construct, I can see where it works very nicely here. It checks the condition after executing the statements inside the loop. I think this is somewhat like Bash's do-until structure (although I've never used that). Just thought I'd share. Now onto the other problems in the chapter.