473,903 Members | 5,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Any way to take a word as input from stdin ?

I searched the c.l.c archives provided by Google as Google Groups with
"word input" as the key words and did not come up with anything good.
C++ has std::string for taking a word as input from stdin. C takes input
in 2 ways:

1) as a character, etchar()
2) as a whole line, fgets()
as C programmer, are we supposed to create a get_word function everytime
when we need a words as input from stdin ( e.g. terminal)
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 10 '08
209 8937
arnuld wrote:
>CBFalconer wrote:
.... snip ...
>
Now there is a big problem in this. In C++ i don't have to care
whether users enter one word or 100s. Memory was being managed by
std. lib. vector. Now here I am thinking of using fgets() to
store the input, which has 2 problems:

1) extract words from each line.
2) fgets() uses array top store data and I don't know how large
is the input, so I can't decide on the size of the array.
My suggestion is to use ggets, available in std. C source code at:

<http://cbfalconer.home .att.net/download/ggets.zip>
>
>Don't forget to preserve the exit char.
Something else may need it.

you mean null character ?
No. I mean the char that doesn't belong to the word and signifies
the completion. It you are getting the word from a string put the
char back by backing up the pointer (or index). If coming from a
stream you have ungetc available.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Sep 11 '08 #21
arnuld wrote:
>
.... snip ...
>
I have not checked it but will be doing it later. The only one
question that keeps on popping up into my mind is "Why C was not
designed to have this feature ? ". That reminds of an article
"Back to Basics" by Joel Spolsky where he said that we have null
terminated strings in C whihc are much slower than PASCAL
strings not by choice but by force, as C was developed on PDP-7,
which had ASCIZ table, which required strings to be Z terminated
( Z means ZERO). Do we have same kid of thing here in my problem?
Speed depends on use. Most string processing just processes until
you hit the end of the string, and there is then no slowdown from
nul termination. In addition most strings are short, and again
there is little effort in finding length. With a little care you
can often avoid finding string lengths in advance.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Sep 11 '08 #22
arnuld wrote:
I have not checked it but will be doing it later. The only one question
that keeps on popping up into my mind is "Why C was not designed to have
this feature ? ".
Because C was designed for /implementing/ this feature; as a bare-bones
systems programming language.
That reminds of an article "Back to Basics" by Joel
Spolsky where he said that we have null terminated strings in C whihc
are much slower than PASCAL strings
I'd be interested in real evidence for this claim. Real, as in, it
happened in these programs and couldn't be eliminated by straightforward
fixes, rather than contrived examples or beginners gotchas.
not by choice but by force, as C was
developed on PDP-7, which had ASCIZ table, which required strings to be Z
terminated ( Z means ZERO).
That seems ... unlikely ... to me. Just because one's assembler has
an ASCIZ directive doesn't mean one has to use it; even if one does,
one can perfectly well also associate a length with a string as well
as a null terminator.
Do we have same kid of thing here in my
problem ?

I am just curious and feel a little strange on having this "word problem"
in C.
You've picked a language deliberately sparse in built-in features;
don't be surprised if it doesn't have many.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Sep 11 '08 #23
On Thu, 11 Sep 2008 12:00:01 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>arnuld said:

<snip>
>Now here I am thinking of using fgets() to store the input,
which has 2 problems:

1) extract words from each line.
2) fgets() uses array top store data and I don't know how large is
the input, so I can't decide on the size of the array.

This is a common problem - so common, in fact, that I wrote it up on the
Web. Take a look at http://www.cpax.org.uk/prg/writings/fgetdata.php which
looks at scanf, gets, and fgets, points out the difficulties with each,
and then discusses a possible solution to the problem of arbitrarily long
lines.

On that page, I present code for reading a word at a time, and for reading
a line at a time. In fact, since you supply your own delimiters, reading a
line is really just a special case of reading a word!

I do not pretend that my code is perfect. For example, the return values
could have been better chosen (I must fix that one day).

It is not intended to be a plug-in solution to the problem (although some
people do actually use it that way and, as far as I'm aware, no harm has
come to them as a result). Rather, it is intended to demonstrate one
possible approach to the problem, in the hope that the reader will have an
"aha!" moment and perhaps come up with a solution that fits his own needs
much better than a generic solution is likely to be able to do.

Several other approaches apart from the one I chose to demonstrate are also
discussed (but not demonstrated), the intent being to give a wider view of
various ways to tackle this problem, depending on your priorities.

Finally, the page provides links to a few other people's demonstrations of
how to solve this problem, again with the intent of providing a wider
perspective on different approaches.
Thank you so much! This is much more the sort of thing I was hoping to
find when I started reading this group.

I much appreciate the excellent documentation in the function itself.

Is there at least an index to other similar solutions to general
problems? In comp.lang.perl. misc one often sees people scolded for not
using tested, robust solutions, rather than reinventing the wheel. CPAN
largely fills most peoples' needs. At the risk of making myself a
complete bore, I ask again: why doesn't the C community follow this
example?

Now, if you just followed the same indenting and bracketting style that
is used in K&R2, I would be *totally* happy. I have a lot of trouble
reading yours. Nevermind, I'll just have to write a perl script to
convert from your style to theirs. Shouldn't be too hard.

Thank you again!!

Sep 11 '08 #24
Pilcrow said:

<snip>
Is there at least an index to other similar solutions to general
problems?
http://www.google.com :-)

<snip>
Now, if you just followed the same indenting and bracketting style that
is used in K&R2, I would be *totally* happy.
Yes, but I wouldn't.
I have a lot of trouble reading yours.
You may well be the first person ever to say that. People have made all
kinds of complaints about my code, but readability is not usually high on
the hit-list.
Nevermind, I'll just have to write a perl script to
convert from your style to theirs. Shouldn't be too hard.
man indent

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 11 '08 #25
Pilcrow <Pi******@gmail .comwrites:
On Wed, 10 Sep 2008 22:05:46 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>>Pilcrow said:
>>On Wed, 10 Sep 2008 15:08:40 +0500, arnuld <su*****@invali d.address>
wrote:
<snip>
>>>>as C programmer, are we supposed to create a get_word function everytime
when we need a words as input from stdin ( e.g. terminal)

Try using fgets(), and strtok(). strtok() will allow you to define word
separators to your taste.

This is poor advice for a beginner. Whilst strtok does have its uses, it
also has issues - traps for the unwary programmer. These derive from its
maintenance of significant state between calls, which makes it unsuitable

I understood that, and I am a 'beginner'. It is very adequately covered
in textbooks (see 'C in a Nutshell', ISBN 0-596-00697-7, page 440),
somewhat less so in K&R2. And I gave the questioner an example to help
him. My dissatisfaction with strtok() is that repeated separation
characters are treated as one, making it difficult to present the user
with an intuitively understandable interface. It is not usually a good
idea to equate ignorance and stupidity.
Yes, it certianly is. Did someone do that?
>>for use in library functions.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 11 '08 #26
arnuld <su*****@invali d.addresswrites :
>On Wed, 10 Sep 2008 17:01:53 -0500, Gordon Burditt wrote:
>The first step is to define what a "word" is.

Fore *my* program, a word is a collection of letters, numbers or anything
separated by space, tab or newline.
As you know, that definition is fine for your program; others might
have different requirements.

Incidentally, the phrase "letters, numbers, or anything" seems
redundant. I think that a more precise rendering of what you meant
would be:

A "word" is a non-empty contiguous sequence of characters other
than space, tab, or newline, preceded or followed either by a
space, tab, or newline or by the start or end of the input.

It would also be good to specify whether the input is a string, a line
of text, or an entire text file.

If I take your definition literally, then in the following
"word"
the word "word" is not a word, because it's not separated by space,
tab, or newline.

It might be more convenient to treat anything for which isspace()
returns true (or for which isspace() returns true in the "C" locale)
as a separator; that includes several whitespace characters that you
didn't mention. But of course if your requirements call for only
space, tab, and newline to be treated as separators, then that trumps
convenience.
>How many words are these:

1. don't

1 word

>2. antidisestablis h-mentarianism

1 word
In the previous article, "antidisestabli sh-" and "mentariani sm" were
on two lines, so they'd be two words by your definition. (Gordon's
point was that it's reasonable to treat them as a single word, since
that's what the hyphen means in English text, but if they're two words
by your definition then they're two words by your definition.)

[snip]
Any collection of letters,symbols or numbers separated by single or
multiple spaces or tab or newline. Therefore

comp.lang.c++ -- 1 word
Std. Lib -- 2 words
Lov@389&om -- 1 word
I think it is pretty much clear now what a word is.
It's pretty much clear what your definition of a word is. It's still
not at all clear what a word is in general (and it can't be, since the
term is used inconsistently) .

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 11 '08 #27
On Thu, 11 Sep 2008 13:24:52 -0700, Keith Thompson <ks***@mib.or g>
wrote:
>Pilcrow <Pi******@gmail .comwrites:
>On Wed, 10 Sep 2008 22:05:46 +0000, Richard Heathfield
<rj*@see.sig.i nvalidwrote:
>>>Pilcrow said:
On Wed, 10 Sep 2008 15:08:40 +0500, arnuld <su*****@invali d.address>
wrote:
<snip>

>as C programmer, are we supposed to create a get_word function everytime
>when we need a words as input from stdin ( e.g. terminal)

Try using fgets(), and strtok(). strtok() will allow you to define word
separators to your taste.

This is poor advice for a beginner. Whilst strtok does have its uses, it
also has issues - traps for the unwary programmer. These derive from its
maintenanc e of significant state between calls, which makes it unsuitable

I understood that, and I am a 'beginner'. It is very adequately covered
in textbooks (see 'C in a Nutshell', ISBN 0-596-00697-7, page 440),
somewhat less so in K&R2. And I gave the questioner an example to help
him. My dissatisfaction with strtok() is that repeated separation
characters are treated as one, making it difficult to present the user
with an intuitively understandable interface. It is not usually a good
idea to equate ignorance and stupidity.

Yes, it certianly is. Did someone do that?
How many times does someone here say, in effect, "this is too deep for a
beginner"?
Sep 12 '08 #28
On Thu, 11 Sep 2008 16:42:51 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>Pilcrow said:

<snip>
>Is there at least an index to other similar solutions to general
problems?

http://www.google.com :-)

<snip>
>Now, if you just followed the same indenting and bracketting style that
is used in K&R2, I would be *totally* happy.

Yes, but I wouldn't.
>I have a lot of trouble reading yours.

You may well be the first person ever to say that. People have made all
kinds of complaints about my code, but readability is not usually high on
the hit-list.
I apologize. It was not really a complaint, more an expression of my
frustration.

I am still digesting that code. I was especially taken with the memory
management. It should be provided for all the other situations where
one sees the a caution that one should make sure that there is adequate
room for the result. After I have gotten more experience with C, I
think I'll try my hand at it.
Sep 12 '08 #29
Pilcrow <Pi******@gmail .comwrites:
On Thu, 11 Sep 2008 13:24:52 -0700, Keith Thompson <ks***@mib.or g>
wrote:
>>Pilcrow <Pi******@gmail .comwrites:
[...]
>>It is not usually a good idea to equate ignorance and stupidity.

Yes, it certianly is. Did someone do that?
s/certianly/certainly/
How many times does someone here say, in effect, "this is too deep for a
beginner"?
That's not equating ignorance and stupidity; it's equating ignorance
and ignorance. And ignorance isn't necessarily an insult; it's
usually curable, after all.

Sorry, but some things really are too deep for a beginner.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #30

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

Similar topics

11
6090
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h>
9
2626
by: Limpor | last post by:
Hi there, I'm very new to C program, I'm about to write a function that read input from standard input, and then returns a pointer to the next word, then i can use it later to print out all the words, but when i run it, i got "Segmentation fault " at the end of output line, this is my code, I've been working on this for a few days, but I still cannot get right, can someone can help me? please! #include <stdio.h> #include <stdlib.h> #include...
27
3175
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
3
3186
by: Sevla | last post by:
hello, im having a problem to change a particular word in a line, can´t find where is my mistake also i would like to know how to make this program check automatically if a particular word exists on the line you got to you exchange it or not i didnt find the right condition to do it
0
10004
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
11293
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10882
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10989
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
10504
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
9692
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
8057
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
6100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3327
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.