By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Hints needed in making a counting of words function.

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.