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 ?

#include<iostream>
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;
  gets(array1);
word();

    return 0;
}


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

            while ( cnt = strstr( cnt, search ) ) {
				++count;
				++cnt;
            }
			}
    cout<<count;
}

please, help me :(

Edited 6 Years Ago by nora-s: n/a

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

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 article has been dead for over six months. Start a new discussion instead.