Solved

how do i count the same word ?

Posted on 1998-11-02
4
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

623 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