Solved

how do i count the same word ?

Posted on 1998-11-02
4
221 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
Comment Utility
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
Comment Utility
thanks for telling me the important part i already get the information and test successfully.

0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
Comment Utility
strstr is the way to go...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now