472,337 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 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 1754
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...
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...
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...
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...
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...
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...
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: ...
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....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.