Solved

how do i count the same word ?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 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