hi : )
I wrote this code for count a specific word selected by user in an array
but it have a logic error ..
and also is there another way to write it without using pointer ?

using namespace std;

int j,i=0;
const int size=500;
char array1[size],search[100],*cnt;


void word();//count a specific word select by user

int main()
  cout<<"****  ..enter your text..  ****"<<endl;

    return 0;

void word()
    int count=0;
	cout<<"enter the word that you want to count"<<endl;
            for ( i = 0; i <= 100; ++i ) {
				cnt = &array1[ i ];

            while ( cnt = strstr( cnt, search ) ) {

please, help me :(

Edited by nora-s: n/a

6 Years
Discussion Span
Last Post by NathanOliver

Look up strings and their manipulative functions. You should be able to split the string into its separate words and count the result.

Votes + Comments
Sounds like a plan

Your solution does have a logical error.

Here's what you can think of as an algorithm.

1. set pointer to start of array.
2. use strstr( ) to find the first instance of the word.
3. When the word is found, increment the counter and then the pointer should be then set to the next position to what has been returned from strstr( ).
4. Repeat this in the process (from 2-3) the pointer is set to NULL or 0.

You will realise that the first for loop is unnecessary in your code.


Hey, I need help with the same thing except a hit counter for items opened within the program.
It doesn't work at all, and I really need help.


@ AveBik Start a new thread with your code you have so far and what is wrong with it plus all relevant error codes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.