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:
(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.
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.
Location: Asia Pacific, Cyberspace, in the Dark Dystopia
Posts: 19,118
Thanks Given: 2,351
Thanked 3,359 Times in 1,878 Posts
Quote:
Originally Posted by deckard
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)