473,797 Members | 3,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stroustrup section 1.5.4, word counting

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 represented as '\n'
2. a tab as '\t'
3. a space as ' '

what i want to know is whether a newline, a space and a tab are
represented internally as characters ?

i know everything is represented as machine's character set, most
probably ASCII where 'A' is 65 but i am actually confused on this
'\t', '\n' , ' ', and character issue.

any help

here is the code that counts characters,word s,tabs and newlines:

// word counting
#include <stdio.h>

#define IN 0
#define OUT 1

int main(void) {
int c, nl, nw, nc, state;

state = OUT;
nl = nc = nw = 0;

while((c = getchar()) != EOF)
{
++nc;

if (c == '\n')
++nl;

if( c == ' ' || c == '\n' || c == '\t')
state = OUT;

else if (state == OUT)
{
state = IN;
++ nw;
}
}

printf("%d NEWLINES \t %d WORDS \t %d CHARs \n", nl, nw, nc);

return 0;
}

Mar 9 '07 #1
3 1875
arnuld wrote:
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 represented as '\n'
2. a tab as '\t'
3. a space as ' '

what i want to know is whether a newline, a space and a tab are
represented internally as characters ?
It depends on the machine and it's character set.
i know everything is represented as machine's character set, most
probably ASCII where 'A' is 65 but i am actually confused on this
'\t', '\n' , ' ', and character issue.

any help
Generally end-of-line sequence is represented by one or two
characters. Under UNIX it's a single linefeed character, while under
DOS-like systems it's a carriage-return followed by a linefeed. MacOS
used to use a single carriage-return. Doubtless other systems may use
more variations.

Spaces and tabs are usually represented by one character.
here is the code that counts characters,word s,tabs and newlines:

// word counting
It's better to use /* ... */ style comments, especially when you're
posting code onto Usenet.

Mar 9 '07 #2
"arnuld" <ge*********@gm ail.comwrites:
[snip]

You mean K&R, not Stroustrup.

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #3
"santosh" <sa*********@gm ail.comwrites:
arnuld wrote:
>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 represented as '\n'
2. a tab as '\t'
3. a space as ' '

what i want to know is whether a newline, a space and a tab are
represented internally as characters ?

It depends on the machine and it's character set.
>i know everything is represented as machine's character set, most
probably ASCII where 'A' is 65 but i am actually confused on this
'\t', '\n' , ' ', and character issue.

any help

Generally end-of-line sequence is represented by one or two
characters. Under UNIX it's a single linefeed character, while under
DOS-like systems it's a carriage-return followed by a linefeed. MacOS
used to use a single carriage-return. Doubtless other systems may use
more variations.
[...]

But C's I/O routines, when operating on files opened in text mode,
hide those details for you. Regardless of how an end-of-line is
represented in an external file (and there are a *lot* of ways to do
this, including fixed-length records with no specific marker), it's
mapped to a single '\n' character.

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #4

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

Similar topics

3
1450
by: arnuld | last post by:
here is the code from section 2.5.1 from Stroustrup (Special Edition): namespace Stack { struct Rep; // definition of stack layout is elsewhere typedef Rep& stack; stack create(); // make a new stack void destroy(stack s); // delete s void push(stack s, char c); // push c onto s
11
2138
by: arnuld | last post by:
this is the code which runs without any trouble: ----------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Entry { std::string name; int e_num;
7
2351
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal? which calls cause the compiler to to introduce a temporary variable? solution: this is the code ----------------------------------------------------------- #include <iostream> void f(char) {};
14
4969
by: arnuld | last post by:
Stroustrup starts chapter 6 with a programme for desk-calculator: here is a grammer for the langugae accepted by the calcualtor: program: END // END is end-of-input expr_list END expr_list: expression PRINT // PRINT is semicolon expression PRINT expr_list
14
2330
by: arnuld | last post by:
i have 2 problems: 1.) in section 4.2 he uses: bool is_open(File*) i want to know why he uses the pointer, instead of these 2: bool is_open(File) or bool is_open(File&)
14
2481
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT: Read a sequence of words from the input. use "quit" as the word
27
2457
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11 STATEMENT: Read a sequence of words from the input. use "quit" as the word to terminate the input. Print the words in the order they were entered. don't print a word twice.modify the programme to sort the
5
1621
by: Wayne Shu | last post by:
Now I'm reading Stroustrup's The C++ Programming Language(Special Edition). In section 4.4 Integer Types, he has wrote that "Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules". I can't understand the two sentences.
6
2480
by: arnuld | last post by:
This one works to seem fine. Can I make this program better ? 1) the use of get(ch) function was inspired from Stroustrup 21.5.1, page number 638. 2) I see, when you create an object of std::ifstream while passing a pointer to it, it automatically opens the file. 3) If I open a file using std::ostream, then I am confused whether it will open the file for writing or appending ?.
0
9685
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
9536
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,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9063
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
7559
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
6802
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
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.