Get successive characters in array

 
How do I iterate through array to get successively repeating characters and possibly get the frequency of occurrence
 
  1. The question makes no sense. That are no “repeating characters” unless your array is a type char.
  2. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

    Fix your broken code.

 
Bucali:
How do I iterate through array to get successively repeating characters and possibly get the frequency of occurrence

Successive doubles ? like how often "AB" (a followed by b) occurs etc ? 

You should maintain a structure array of combinations , this would log each occuring combo and its # of appearances .

Then you need a search function , i.e find if you have logged the combination ,and an add function , so if you have not logged it you add it to the "collection of combinations".

You then iterate from the first character to the one before the last and for that character and the one next to it you 

  • Search for combination in the collection.
  • If it does not exist log it 
  • if it exists increase its # of appearances 
/*
the structure containing the combinations
the following represents one item
*/
struct dual_combo{
       private:
string combo;
int    appearances;
       public:
       dual_combo(void){combo=NULL;appearances=0;}
       dual_combo(string _combo,int _apps){set(_combo,_apps);}
  void set(string _combo,int _apps){combo=_combo;appearances=_apps;}
  void increase(){appearances++;}  
   int times_occured(){return(appearances);}
string get_combo(){return(combo);}
  bool is_equal(string _with){if(_with!=NULL&&_with==combo){return(true);}return(false);}
  bool operator==(string _with){return(is_equal(_with));}
  bool operator!=(string _with){return(!is_equal(_with));}
};
/* 
the log of found dual combinations
the items from above are stored on a "list" here
*/
struct combos_log{
//the array of dual combo items 
  dual_combo combos[];
             //
             combos_log(void){ArrayFree(combos);}
            ~combos_log(void){ArrayFree(combos);}
        void reset(){ArrayFree(combos);}
             //the search function , you call this to see if your combo has been logged already 
         int search(string _for_what){
             //loop into your existing combos and compare them with what you are looking for to return the position (if it exists)
             for(int i=0;i<ArraySize(combos);i++){
             if(combos[i]==_for_what){
             //if you find it , return the position i in the combos list 
             return(i);
             }}
             return(-1);//if it does not exist return -1
             } 
             //the add new combo function 
         int add(string _new_combo,int new_appearances){
             //the new size of your array (the collection of combos) will be existing + 1 
             int ns=ArraySize(combos)+1;
             //resize the list 
             ArrayResize(combos,ns,0);
             //add the new item in the latest position 
             combos[ns-1].set(_new_combo,new_appearances);
             return(ns-1);//return the position of the newly logged combo 
             }
             //increase the appearance of a specific combo , a combo that is on index ix in your array
        void increase(int ix){
             if(ix>=0&&ix<ArraySize(combos)){
             combos[ix].increase();
             }}
};
/*
You have the structure types now you need to create the object of the list.
so combols log type , like variables (i.e. double price=...;)
*/
combos_log LOG;
int OnInit()
  {
//--- create timer
   //reset log
     LOG.reset();
   string book="ABBACSDARWDWAGABB";
   //loop to the characters minus 1
     for(int i=0;i<StringLen(book)-1;i++)
     {
     //a combo candidate is on this character and the next 
       string combo_candidate=StringSubstr(book,i,1)+StringSubstr(book,i+1,1);
     //search for that combo in the collection 
       int find=LOG.search(combo_candidate);//we are looking for the combo candidate 
       //if we did not find it (-1)
       if(find==-1){
       //we add it and assign the ix to find 
       find=LOG.add(combo_candidate,1); 
       }  
       else{//if we found it 
       LOG.increase(find);
       }
     }
   //test print all combos 
     for(int i=0;i<ArraySize(LOG.combos);i++)
     {
     Print("COMBO["+IntegerToString(i)+"]["+LOG.combos[i].get_combo()+"]#("+IntegerToString(LOG.combos[i].times_occured())+")");
     }
   
//---
   return(INIT_SUCCEEDED);
  }
 
William Roeder #:
  1. The question makes no sense. That are no “repeating characters” unless your array is a type char.
  2. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

    Fix your broken code.

You are not helping stop what you are doing if you don't have an answer ignore and skip the question. you must be banned

 
Bucali:
How do I iterate through array to get successively repeating characters and possibly get the frequency of occurrence

Loop through the char array and compare - example attached to get you going

//+------------------------------------------------------------------+
//|                                      428530-SuccessCharCount.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

//https://www.mql5.com/en/forum/428530/

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   char   charArray[]   = {'a','a','B','b','b','c','d','a','b','E','e','z','Z','y','y','q'};
   Print("\n ---- charStr = " + CharArrayToString(charArray));

   Print("\n ---- countSuccChars(charArray, ignorecase=false);");
   countSuccChars(charArray, false);
   Print("\n ---- countSuccChars(charArray, ignorecase=true);");
   countSuccChars(charArray, true);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
void countSuccChars(char   &charArray[], bool ignoreCase)
//+------------------------------------------------------------------+
  {

   string thisChar, prevChar;
   string succChars = "";
   int    count     = 1;

   for(int i = 1; i < ArraySize(charArray); i++)
     {
      prevChar = CharToString(charArray[i - 1]);
      thisChar = CharToString(charArray[i]);

      //Ignore case
      if(ignoreCase)
        {
         StringToLower(prevChar);
         StringToLower(thisChar);
        }

      if(i == 1)
        {
         succChars = StringFormat("[%d]%s", i-1, prevChar);
        }
      if(thisChar == prevChar)
        {
         succChars += StringFormat("[%d]%s", i, thisChar);
         count++;
        }
      else
        {
         if(count > 1)
            PrintFormat("Successive char found[count=%d]: %s", count, succChars);
         succChars = StringFormat("[%d]%s", i, thisChar);
         count = 1;
        }
     }
//+------------------------------------------------------------------+
  }
//+------------------------------------------------------------------+
Reason: