Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do i count the same word ?

Posted on 1998-11-02
4
Medium Priority
?
238 Views
Last Modified: 2010-04-02
i already write a program that count how many word in that sentence
#include <stdio.h>
#include <string.h>
main()
{
char string [500];
char *token;
int count;
printf("please enter your note:\n");
gets (string);
token=strtok (string," ");
while (token != NULL)
{
// printf("%s\n",token);
token=strtok (NULL," ");
count++;
}
printf ("\n%d%5s\n\n",count,"word");
return 0;
}
for example the output
please enter your note:
i go to school i still go to school tomorrow
10 words

on the output i just can count that the sentence got 10 words but my question is how do i count the "go" is 2 and the "to" is 2 when user just want to count how many "go" and "to" in the sentence
i thinks to use the strcmp to compare each word but the "go" and "to" are the same (2 characters) so will return 0 and can't count  the different word "go" and "to".

thanks for helping me
0
Comment
Question by:wongmlai
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 0 total points
ID: 1176806
1. you misunderstand strcmp - it does not compare the length, it compares the value of the string.  so "go" and "to" are not the same

2. try using strstr - this will find the location of one string inside another.  it finds the first occurence if the word occurs multiple times.  Therefore to find the next occurrence start counting at the pos it left off

For example
char szBuffer[] = "i go to school tomorrow" ;
char * pstr = strstr( szBuffer, "go" ) ; /* will find the first "go"  - or pstr will be NULL if not found */
if one match is found, then find the next one using
pstr = strstr( pstr+1, "go" ) ; /* NULL or position of next match */

I don't want to give you the code as this is a school assignment.  This should could you enough clues to get started.  If you are still struggling, edit the code using the info I gave you and post a follow-up question in a comment
0
 

Author Comment

by:wongmlai
ID: 1176807
thanks for telling me the important part i already get the information and test successfully.

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1176808
1. Don't be afraid to ask follow up questions
2. If you get stuck post your code as you progress
3. Good luck :-)
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1176809
strstr is the way to go...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question