Hey guys,

I want to know one thing, for instance I am having a character array like this: [@@123124123123125@@@] where 123, 124 and 125 are one three-digit numbers and are NOT separate like 1,2 and 3. I want to know the number of times 123, 124 and 125 have occurred. This is my attempt so far:

for (int i=2; i<count-3; i++) {
          for(j=i+3; j<count-3; j++) {
          if ((dataPacket[i]==dataPacket[j]) && (i!=j))
         {
           counting=counting+3;
         }
       }
     }

This is giving me huge random numbers and considering 1, 2 and 3 separately. Any help is highly appreciated.

Thanks

can you post the code fully ?

Hint: convert the numbers in the character array into integers. This would give you a good start.

This is just so you can get the idea.You should use higher constructs like strings vectors and stringstreams.

#include <cctype>
#include <cstdio>

int main ()  {
   char arr[]={"@@123124125@@@"};

   int packcount=0;
   int lettercount=0;

   char pack[10][3]={0};

   for (int x=0;x<sizeof(arr);x++)  {
      if (std::isdigit(arr[x]))  {
         pack[packcount][lettercount]=arr[x];
         lettercount++;
       }
      if (lettercount==3)  {
         packcount++;
         lettercount=0;
       }
    }
  std::printf ("\n%s",pack[2]);
 }

Thanks but I want to implement it without using standard libraries. Any ideas for that?

You could enhance the above algorithm like structuring your program in a more easy to grasp manner.For example you could define a structure say CharStream to hold the input array and another class called Packet for which to overload the insertion operator of class CharStream to perform the actual insertion something similar to the way istringstream performs insertion.

friend CharStream& operator >> (CharStream& i,Packet& p)  {
   //here is where you copy the characters
 }
CharStream i(input);
Packet p;
i >> p;

Edited 5 Years Ago by caut_baia: n/a

As I said, is there any way to implement without using libraries or even friend functions.. that would be better.

With such restrictions given and with speed of current computers as it is, I would say that you just do it simply, primitively and fast enough (I do know more C than C++):

  1. Do struct with 3 letter id and count for enough numbers
  2. From first number step forward by threes and increase count for that number (put id to next available struct and advance last used struct counter if not yet encountered)
This article has been dead for over six months. Start a new discussion instead.