Sponsored Content
Top Forums Programming C Beginner Looking For Suggestions Post 302302617 by deckard on Tuesday 31st of March 2009 12:40:42 PM
Old 03-31-2009
Question 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:

Code:
#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.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Suggestions on where to begin?

I have been a student at Hendrix Institute for about a year now. My term is comming to an end by the end of december. I have learned varios computer programs for web development that include Flash 5 and Dreamweaver. Actionscripting, Javascript and Database development with Access was all... (4 Replies)
Discussion started by: andrew25008
4 Replies

2. UNIX for Dummies Questions & Answers

Suggestions wanted ...

All, Have an AMD-K6/2 PC, 20G.Hd along with RH7.2. Wanting to know what I should do in terms of setup (workstation/server) and then what I can do with it? I'd like to learn a DBMS and SQL - can I do this using RedHat? Any suggestions with how I can use/ what I can do with this appreciated. (3 Replies)
Discussion started by: Cameron
3 Replies

3. UNIX for Dummies Questions & Answers

Backup suggestions

The current backup procedure we using a tar command in linux. The files are stored in one partition in different folders. The docs stores in day wise folders like ex: /usr/data/xyz/20050129, /usr/data/xyz/20050130 .............etc We using tar & gzip command to take backup everyday. The backup... (3 Replies)
Discussion started by: bache_gowda
3 Replies

4. UNIX for Advanced & Expert Users

Looking for Suggestions...

We run WebSphere and by default it wants to install everything under /usr. While I can understand the default (everyone has a /usr) I would like to move this over to a dedicated volume group called apps and then setup my lv's and fs's here. Our WebSphere Admin doesn't like this because apparently... (1 Reply)
Discussion started by: scottsl
1 Replies

5. Solaris

Suggestions Req

Hi all, I have worked on HP UNIX and now i have moved to SunSolaris which i never used to work. I am more on programming side like shell and perl scripting. So i want to know from you experts that i need to take care or changes which i code in sun solaris in compared to HP unix. Suggestions... (1 Reply)
Discussion started by: ravi.sadani19
1 Replies

6. Shell Programming and Scripting

Suggestions on input

Hi, I have written a script which calls a process which ends up in a reboot of the system. At the end of the reboot it prompts for login & i need to provide the login details. am not able to figure out hw to do this. Doubt: will echoing login details after calling the process work? for ex:... (1 Reply)
Discussion started by: meera
1 Replies

7. UNIX for Advanced & Expert Users

Need suggestions.......

Hello there....i am a final year comp science student.......i am thinking of doing my project on unix platform......which one do u suggest?thanx in advance... (3 Replies)
Discussion started by: theprasad1990
3 Replies

8. Solaris

Suggestions for new T5140

I've been busy and fell behind on Sun/Oracle. Forgive me if too basic. I welcome brief, cryptic, or advanced replies. I also welcome noobie information since I may have no clue what's up at the moment. Problem statement: I inherited a computer to set up. I would rather not figure out 8 months... (1 Reply)
Discussion started by: Nevyn
1 Replies

9. UNIX for Dummies Questions & Answers

Suggestions for the interview

Hi guys, i'm undergoing a traning in solaris administration and i request if any one have an idea on the interview questions on solaris. thank you. (3 Replies)
Discussion started by: 038karthik
3 Replies

10. Shell Programming and Scripting

Suggestions on this script please

i=1 out="" j=`expr 2 * $1` while do out="$out"#"" echo $out ((i=i+1)) done while do print ${out%?} ((i=i+1)) done This script is throwing an error: gurnish:/home/fnb/gurnish/saurabh/scripts> while1 3 expr: 0402-050 Syntax error. # (6 Replies)
Discussion started by: targetshell
6 Replies
ARITHMETIC(6)							   Games Manual 						     ARITHMETIC(6)

NAME
arithmetic - provide drill in number facts SYNOPSIS
/usr/games/arithmetic [ +-x/ ] [ range ] DESCRIPTION
Arithmetic types out simple arithmetic problems, and waits for an answer to be typed in. If the answer is correct, it types back "Right!", and a new problem. If the answer is wrong, it replies "What?", and waits for another answer. After every twenty problems, it publishes statistics on correctness and the time required to answer. To quit the program, type an interrupt (delete). The first optional argument determines the kind of problem to be generated; +-x/ respectively cause addition, subtraction, multiplication, and division problems to be generated. One or more characters can be given; if more than one is given, the different types of problems will be mixed in random order; default is +-. Range is a decimal number; all addends, subtrahends, differences, multiplicands, divisors, and quotients will be less than or equal to the value of range. Default range is 10. At the start, all numbers less than or equal to range are equally likely to appear. If the respondent makes a mistake, the numbers in the problem which was missed become more likely to reappear. As a matter of educational philosophy, the program will not give correct answers, since the learner should, in principle, be able to calcu- late them. Thus the program is intended to provide drill for someone just past the first learning stage, not to teach number facts de novo. For almost all users, the relevant statistic should be time per problem, not percent correct. 7th Edition May 6, 1986 ARITHMETIC(6)
All times are GMT -4. The time now is 08:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy