473,782 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2 - section 1.5.3 , exercise 1-8

as i said, i have restarted the book because i overlooked some
material. i want to have some comments/views on this solution. it runs
fine, BTW.

------------------ PROGRAMME --------------
/* K&R2 section 1.5.3, exercise 1-8

write a programme to count blanks, tabs and newlines

*/
#include <stdio.h>

int main()
{
int c;
int ns = 0; /* number of SPACES */
int nt = 0; /* number of TABS */
int nn = 0; /* number of NEWLINES */

while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;

if(c == '\t')
++nt;

if(c == '\n')
++nn;
}

printf("SPACES: %d\nTABS: %d\nNEWLINES: %d\n", ns, nt, nn);

return 0;
}

---------- OUTPUT ----------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-8.c
[arch@voodo kr2]$ ./a.out
like
SPACES: 0
TABS: 0
NEWLINES: 1
[arch@voodo kr2]$ ./a.out
like this
and this this this
SPACES: 2
TABS: 2
NEWLINES: 2
[arch@voodo kr2]$

Mar 20 '07 #1
4 1571
On Mar 20, 4:16 am, "arnuld" <geek.arn...@gm ail.comwrote:
as i said, i have restarted the book because i overlooked some
material. i want to have some comments/views on this solution. it runs
fine, BTW.

------------------ PROGRAMME --------------
/* K&R2 section 1.5.3, exercise 1-8

write a programme to count blanks, tabs and newlines

*/

#include <stdio.h>

int main()
{
int c;
int ns = 0; /* number of SPACES */
int nt = 0; /* number of TABS */
int nn = 0; /* number of NEWLINES */

while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;

if(c == '\t')
++nt;

if(c == '\n')
++nn;
}

printf("SPACES: %d\nTABS: %d\nNEWLINES: %d\n", ns, nt, nn);

return 0;

}

---------- OUTPUT ----------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-8.c
[arch@voodo kr2]$ ./a.out
like
SPACES: 0
TABS: 0
NEWLINES: 1
[arch@voodo kr2]$ ./a.out
like this
and this this this
SPACES: 2
TABS: 2
NEWLINES: 2
[arch@voodo kr2]$
My only comment is:
"Nicely done."

Mar 20 '07 #2
On Mar 21, 12:06 am, "user923005 " <dcor...@connx. comwrote:

My only comment is:
"Nicely done."
:-)

Mar 21 '07 #3
On Mar 20, 9:56 pm, "arnuld" <geek.arn...@gm ail.comwrote:
On Mar 21, 12:06 am, "user923005 " <dcor...@connx. comwrote:
My only comment is:
"Nicely done."

:-)
One possible minor improvement is to connect the if()s

while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;
else if(c == '\t')
++nt;
else if(c == '\n')
++nn;
}

The reason it might be considered a minor improvement is that the 2nd
and 3rd tests won't be performed if the previous tests return true.
You could similarly use a switch:

while((c = getchar()) != EOF)
{
switch (c) {
case ' ':
++ns;
break;
case '\t':
++nt;
break;
case '\n':
++nn;
break;
default:
break;
}
}

Which may (or may not) optimize a tiny bit better.
But I don't really prefer either of these two forms over your
original.
The switch form has the benfit that it expands easily to other cases
(e.g. '\r' etc. for whitespace or whatever else you might be
interested in counting.

Another interesting possibility might be to use an array dimentioned
UCHAR_MAX+1.
Then, for all characters that are read, you could just do this:
charlist[c]++;
and when the file was empty, you would have an instant count of any
and all characters.
The nice thing about this one is that it is branchless.
Mar 21 '07 #4
Groovy hepcat arnuld was jivin' on 20 Mar 2007 04:16:52 -0700 in
comp.lang.c.
K&R2 - section 1.5.3 , exercise 1-8's a cool scene! Dig it!
>as i said, i have restarted the book because i overlooked some
material. i want to have some comments/views on this solution. it runs
fine, BTW.
Nice job! A couple of fairly minor niggles, though.
>------------------ PROGRAMME --------------
/* K&R2 section 1.5.3, exercise 1-8
write a programme to count blanks, tabs and newlines
*/

#include <stdio.h>

int main()
{
int c;
int ns = 0; /* number of SPACES */
int nt = 0; /* number of TABS */
int nn = 0; /* number of NEWLINES */
These variables could be better named. Since you're counting the
number of blanks, tabs and newlines, you might call these variables
blanks, tabs and newlines, respectively. It's a small point in a
trivial program such as this one; but in a larger program, you'll
thank yourself for using more descriptive variable names.
while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;
Here you've mixed tabs and spaces in the source code. This is always
a bad idea. What looks right/good in your source code editor may not
look good in my newsreader. (In fact, it doesn't.) Please use only
spaces (prefered) or only tabs for indentation. Many text editors can
be set to insert spaces up to user-defined tab stops instead of a tab.
I guess that's it.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Mar 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2297
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i found the home
8
4763
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
16
1813
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to output replacing
7
2381
by: arnuld | last post by:
this runs fine and does what i want :-) any advice on making it better. ------------ PROGRAMME--------------- /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\.
19
2407
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
5
2919
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any compile-error BUT it has a semantic BUG: what i WANT: I want it to produce a "horizontal histogram" which tells how many characters were in the 1st word, how many characters were in the second word by writing equal number of stars, *, at the...
4
1854
by: arnuld | last post by:
any suggestions for improvement: -------------- PROGRAMME ------------- /* K&R2: section 1.5.3 exercise 1-10 STATEMENT: write a programme to copy its input to output, replacing each TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'. */
88
3797
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without invoking undefined behaviour triggered by overflow? Remember that the constants in limits.h cannot be used.
6
2809
by: arnuld | last post by:
This is the example from section 4.2, page 71 of K&R2: double atof( char s ) { int i, sign; double val, power; for( i = 0; isspace( s ); ++i )
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.