Solved

how do i count the same word ?

Posted on 1998-11-02
4
222 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming on screen browser tutorial lesson 2 76
Dialogbox API leak? 18 81
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 93
Issues with C++ Class 19 81
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

920 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

11 Experts available now in Live!

Get 1:1 Help Now