473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Counting Tabs, New lines, etc. ~error~

Ok, sorry for posting so much, but I just have alot of questions. Ok,
so It says to make a program to count blanks, tabs, and newlines.

So I made one:

#include <stdio.h>

/* Count tabs, newlines and blanks; version 1 */
int main(void)
{
int c, nl, tb, cb;

nl = 0;
tb = 0;
cb = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
if (c == '\t')
++tb;
if (c == '_')
++cb;
printf("%d\n%d\n%d\n", nl, tb, cb);
return 0;
}

Now here's what I don't understand:

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

That(\t) be a tab so when I put a tab in it should make the count 1,
but it doesn't.

And:

if (c == '_')
++cb;

Should add a number to the blank count when you make a space, but it
doesn't. Also I wasn't sure about what the '?' was for a blank
space(eh, what a noob your thinking). Yes, I know: I'm a noob.

Ok well, could someone help me? Again: sorry for posting and asking so
much.

Nov 14 '05 #1
4 2303

Matt wrote:
[ snip! ]
/* Count tabs, newlines and blanks; version 1 */
int main(void)
{
int c, nl, tb, cb;

nl = 0;
tb = 0;
cb = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
if (c == '\t')
++tb;
if (c == '_')
++cb;
printf("%d\n%d\n%d\n", nl, tb, cb);
return 0;
}


Hint: What is the significant difference between
the following bits of code?

/* SAMPLE #1 */
while ((c = getchar()) != EOF) {
/* some stuff */
/* some more stuff */
}
/* SAMPLE #2 */
while ((c = getchar()) != EOF)
/* some stuff */
/* some more stuff */

Nov 14 '05 #2
Wow, that's almost funny the mistake I made. Thanks alot for helping!

Nov 14 '05 #3
"Matt" <no******@gmail.com> writes:
[...]
And:

if (c == '_')
++cb;

Should add a number to the blank count when you make a space, but it
doesn't. Also I wasn't sure about what the '?' was for a blank
space

[...]

No, that increments the blank count when it sees an underscore
character.

The right way to do this is (you're going to slap yourself in the
forehead when you see this):

if (c == ' ')
++cb;

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Matt wrote:
Ok, sorry for posting so much, but I just have alot of questions. Ok,
so It says to make a program to count blanks, tabs, and newlines.

if (c == '_')
++cb;

Should add a number to the blank count when you make a space, but it
doesn't. Also I wasn't sure about what the '?' was for a blank
space(eh, what a noob your thinking). Yes, I know: I'm a noob.


'_' is not a space. It's a different character. You can use
it in variable names, eg:

char this_is_a_char;

If you want to check for a space, use (naturally):

if (c == ' ')

Nov 14 '05 #5

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

Similar topics

17
by: bearophileHUGS | last post by:
Hello, I know this topic was discussed a *lot* in the past, sorry if it bores you... >From the Daily Python-URL I've seen this interesting Floating Point Benchmark:...
19
by: Alex Vinokur | last post by:
Is there any tool to count C-program lines except comments? Thanks, ===================================== Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
15
by: Tom Plunket | last post by:
The documentation for dedent says, "Remove any whitespace than can be uniformly removed from the left of every line in `text`", yet I'm finding that it's also modifying the '\t' characters, which...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
3
by: arnuld | last post by:
this is an example programme that counts lines, words and characters. i have noticed one thing that this programme counts space, a newline and a tab as a character. i know: 1. a newline is...
13
by: rajash | last post by:
Hi I am new to this forum. I have taken a class in C some time ago but now I am reading Kernigan and Richie's book to refresh my knowledge. I think I have forgotten alot and there are no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.