473,626 Members | 3,201 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 #1
209 8713
On Sep 10, 1:08 pm, arnuld <sunr...@invali d.addresswrote:
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)
char word[64];
scanf("%63s", word);

Alternatively, write a get_line function (or use one written by pete,
richard heathfield, eric sossman, cbfalconer et cetera) and then split
that into words.
Sep 10 '08 #2
arnuld <su*****@invali d.addresswrote:
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)
There is no generic solution (mainly because there is no consensus on
what a "word" is), so yes.

Richard
Sep 10 '08 #3

"arnuld" <su*****@invali d.addresswrote in message
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)
Generally there will be a regular expression parser available. It's not part
of the standard library, unfortunately, so the details may vary.
You can specify exactly what you mean by a 'word', and extract with that.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 10 '08 #4
On Wed, 10 Sep 2008 15:08:40 +0500, arnuld <su*****@invali d.address>
wrote:
>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)
Try using fgets(), and strtok(). strtok() will allow you to define word
separators to your taste.

Here is sample code:

------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#define MAXLINE 500

char *tok;
char line[MAXLINE];

int main(void)
{
while(fgets(lin e, MAXLINE, stdin) != NULL) {
if((tok = strtok(line," \n")) != NULL) puts(tok); /* first
token on each line */
while((tok = strtok(NULL," \n")) != NULL) puts(tok); /*
subsequent tokens */
}
return 0;
}
Sep 10 '08 #5
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
for use in library functions.

--
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 10 '08 #6
>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)
The first step is to define what a "word" is.

How many words are these:

1. don't
2. antidisestablis h-
mentarianism
3. Joe,Bob,Sally, and Henry.
4. Joe, Bob, Sally, and Henry.
5. $1,416,383,583. 20
6. ()@#$#^&*#^%%#^ @*^$&*$
7. George W. Bush
8. slam-dunk
9. 15th-century vase
10. M.O.N.S.T.E.R., the computer chess-playing machine
11. lo************* *************** **...**@yaho o.com

Justify your answers.
Sep 10 '08 #7
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.
>for use in library functions.


Sep 10 '08 #8
arnuld wrote:
>
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)
Well, first you have to define a word. Does it terminate on
blanks, on blanks and non-print chars, on blanks and tabs, etc. I
think you will find that the C++ mechanism terminates on blanks and
'\n' (but I could well be wrong). Having defined it, you just
write the code to extract such a beast from a stream (or from a
string). At that point both you and your code reader know exactly
what the function extracts.

Don't forget to preserve the exit char. Something else may need
it.

Note that, having written the function, you are allowed to keep its
source (and its object code) and reuse it as often as you wish,
with minimum effort. If you have taken the elementary precaution
of writing it in standard C, you can use it anywhere.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Sep 11 '08 #9
On 10 Sep, 11:08, arnuld <sunr...@invali d.addresswrote:
>
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)
No. You should either find a function that does what you want
or write it yourself, and once you have done that...don't
ever do it again. Put it in a library and use it.

If you are re-writing the same function repeatedly, then
you aren't a C-programmer. You aren't any kind of programmer.
Re-writing the same functionality can be a useful
exercise for the novice, but it is a silly waste of
time otherwise.

Sep 11 '08 #10

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

Similar topics

11
6069
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
2615
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
3128
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
3170
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
8265
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
8196
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
8705
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
8504
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
7193
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...
0
5574
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
4092
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...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
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 we have to send another system

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.