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,words,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;
} 3 1846
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,words,tabs and newlines:
// word counting
It's better to use /* ... */ style comments, especially when you're
posting code onto Usenet.
"arnuld" <ge*********@gmail.comwrites:
[snip]
You mean K&R, not Stroustrup.
--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
"santosh" <sa*********@gmail.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_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."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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(); //...
|
by: arnuld |
last post by:
this is the code which runs without any trouble:
-----------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
struct Entry {
std::string...
|
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?...
|
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...
|
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&)
| |
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:...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
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...
| | |