473,396 Members | 2,061 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,396 software developers and data experts.

Hints needed in making a counting of words function.

Basically, i want to make a function that will receive a sentence or
phrase, and count its words.

It would start like this (i think):

#include <stdio.h>

int count ( char sentence [] )
{
char string [100];

printf("Enter sentence: ");
scanf() //not sure if this is the right thing to do..
etc...
}

Problem is that i am not sure if there are any functions that make this
easier. I looked at fgets() but that one wants a file. Also, what
happens if someone puts in a long sentence? I mean, the size of the
array would be a fixed size in the function, no?

Any hints are fine, not looking for the entire code.

Thanks.
Apr 24 '06 #1
6 1841
gk245 kirjoitti:
Basically, i want to make a function that will receive a sentence or
phrase, and count its words.

It would start like this (i think):

#include <stdio.h>

int count ( char sentence [] )
{
char string [100];

printf("Enter sentence: ");
scanf() //not sure if this is the right thing to do..
etc...
}

Problem is that i am not sure if there are any functions that make this
easier. I looked at fgets() but that one wants a file. Also, what
happens if someone puts in a long sentence? I mean, the size of the
array would be a fixed size in the function, no?

Any hints are fine, not looking for the entire code.

Thanks.


FILE *stdin; <- standard input, defined in stdio.h

Use that...

Aki Tuomi
Apr 24 '06 #2
gk245 wrote:
Basically, i want to make a function that will receive a sentence or
phrase, and count its words.
Problem is that i am not sure if there are any functions that make this
easier. I looked at fgets() but that one wants a file. Also, what
actually, it takes a FILE *. stdin is a FILE * which you can use for
user input.
happens if someone puts in a long sentence? I mean, the size of the
array would be a fixed size in the function, no?


you can use a loop and call fgets() again until you find the period,
counting the words each time. the strpbrk() function will probably be
helpful too.

Apr 24 '06 #3
gk245 said:
Basically, i want to make a function that will receive a sentence or
phrase, and count its words.


You seem to want to have the function capture the input itself. This will do
it...

#include <stdio.h>
#include <ctype.h>

int countwords(void)
{
int in_a_word = 0;
int ch = 0;
int wcount = 0;

while((ch = getchar()) != '\n')
{
if(isspace(ch) != 0)
{
in_a_word = 1;
}
else
{
if(in_a_word)
{
in_a_word = 0;
++wcount;
}
}
}

return wcount;
}

int main(void)
{
int c = 0;

printf("Type a sentence, and hit ENTER.\n");
c = countwords();
printf("Words: %d\n", c);

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 24 '06 #4
On 2006-04-24, gk245 <to*****@mail.com> wrote:
Basically, i want to make a function that will receive a sentence or
phrase, and count its words.

It would start like this (i think):

#include <stdio.h>

int count ( char sentence [] )
{
char string [100];

printf("Enter sentence: ");
scanf() //not sure if this is the right thing to do..
I don't think you need to have the user type in the sentence-- it looks
like it's already a parameter to the function.

Unless that is what you want, in which case you don't need the
parameter.
Problem is that i am not sure if there are any functions that make this
easier. I looked at fgets() but that one wants a file. Also, what
happens if someone puts in a long sentence?
These are all the right kind of things to be worrying about with user
input :)

You can use fgets with stdin instead of a file, and give it a
max-allowed length.
Any hints are fine, not looking for the entire code.


One of strchr, strcspn or strtok might be useful. I don't mean this as a
cryptic hint, you could use either of the three, or just as easily not
bother with any of them. strchr is a bit limited if words can be
separated with not just space but also tab or newline.
Apr 24 '06 #5
gk245 <to*****@mail.com> writes:
Basically, i want to make a function that will receive a sentence or
phrase, and count its words.

It would start like this (i think):

#include <stdio.h>

int count ( char sentence [] )
{
char string [100];

printf("Enter sentence: ");
scanf() //not sure if this is the right thing to do..
etc...
}

Problem is that i am not sure if there are any functions that make
this easier. I looked at fgets() but that one wants a file. Also,
what happens if someone puts in a long sentence? I mean, the size of
the array would be a fixed size in the function, no?


How does your count() function get the string that it's going to
process? It looks like you're passing it in as a parameter
"char sentence[]" *and* reading it from stdin (char string[100]).
Do one or the other, not both.

The best approach is probably to separate obtaining the input from
processing it. fgets() is a good way to read a line of input. Once
you've called fgets() (check whether it succeeded), you have a pointer
to a string; use that to process the data.

To focus on a relatively minor point, this:
int count(char sentence[]);
is exactly equivalent to this:
int count(char *sentence);

You can't really pass an array directly to a function; you can only
pass a pointer through which you can access the array. C allows the
"[]" syntax in a parameter declaration, which makes it look like
you're passing an array; in my opinion, this feature causes more
problems that it solves, and is best avoided. Learn how arrays and
pointers work (section 6 of the comp.lang.c FAQ covers this very
well), and don't try to pretend that arrays are "really" pointers.

--
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.
Apr 24 '06 #6
gk245 wrote:
Basically, i want to make a function that will receive a sentence or
phrase, and count its words.

It would start like this (i think):

#include <stdio.h>

int count ( char sentence [] )
{
char string [100];

printf("Enter sentence: ");
scanf() //not sure if this is the right thing to do..
etc...
}

Problem is that i am not sure if there are any functions that make this
easier. I looked at fgets() but that one wants a file. Also, what
happens if someone puts in a long sentence? I mean, the size of the
array would be a fixed size in the function, no?

Any hints are fine, not looking for the entire code.

Thanks.


I would always use fgetc() for this purpose:

int ch;
int ctr = 0;
while ((ch = fgetc(stdin)) != '\n')
if (!isapha(ch))
ctr++;

That little bit of code would simply scan the line and increase its
counter each time it hit a non-alphabetic character. Minor modifications
are needed to check for doubles (so that ". " is not considered two
words), but otherwise that'll work fine.

Unless you need to keep the sentence available...
Apr 25 '06 #7

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

Similar topics

4
by: Kim Petersen | last post by:
I've worked on this table object a bit too long - and seem to have stared too long at the code. Can someone see where it goes wrong in the insertrow function? Also optimization hints and...
8
by: regrat | last post by:
It's possible create a javascript which counts all words in a webpage until the point in which I click with the mouse. Any suggests?
8
by: DrNoose | last post by:
Hi! I'm writing a program that is supposed to read a line of input, count the words and the number of occurrences of each letter. Then it should prints the number of occurrences of each letter...
6
by: dixie | last post by:
I have a text field on a form which has names with a comma between them like this: 'J. Smith, A. Jones, A. Man, J. Johns'. I am trying to find a procedure that will count the number of people in...
9
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
11
by: Bonj | last post by:
Hello I am making a syntax highlighter for T-SQL, and I am going to hardcode the words into it for speed's sake (I will probably have thought up enough new features for a new version when the next...
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,...
4
by: Lester | last post by:
I need to fire a function 4 times a second using setTimeout()), but I have an HTML-editor on the page (FCKeditor) which has hints over its buttons. However, the rapid timer never allows these hints...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
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...

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.