Hints needed in making a counting of words function.

G

gk245

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.
 
A

Aki Tuomi

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
 
T

tedu

gk245 said:
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.
 
B

Ben C

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.
 
R

Richard Heathfield

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;
}
 
K

Keith Thompson

gk245 said:
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.
 
A

Andrew Poelstra

gk245 said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top