Count Occurrence of Substring in a String

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  char ptr[ ] = “123abcdabeabf”;
  int count, start, end, up, move, endIndex;
  count = 1;
  start = end = up = 0;
  move = 1;
  endIndex = strlen( ptr ) + 1;
  while( start <= endIndex )
  {
    while( move <= endIndex )
    {
      if( ptr[ up ] == ptr[ move ] )
      {
        end = up;
        if( ptr[ up + 1 ] != ptr[ move + 1 ] )
        {
          count ++;
        }
        up ++;
      }
      else if( ptr[ up ] != ptr[ move ] )
        up = start;
      move ++;
    }
    if( count > 1 )
    cout<<“string: “<<ptr<<endl
      <<“count : “<<count<<endl
      <<“start : “<<start<<endl
      <<“end   : “<<end<<endl
      <<“up    : “<<up<<endl;
    up = start = end = end + 1;
    count = 1;
    move = end + 2;
  }
  cout<<“—>END OF LINE<—“<<endIndex<<endl;
  _gettch( );
  return 0;
}

OUTPUT:
——-
string: 123abcdabeabf
count : 3
start : 3
end   : 4
up    : 3
string: 123abcdabeabf
count : 2
start : 7
end   : 8
up    : 7
—>END OF LINE<—14

Suggest it, how to make it correct?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s