comparing characters in a string
Hello coders
The code below is supposed to take first 2 characters from the main string and put it into string twochars the next 2 characters go into another string secondtwochars. If the charcters of the temp strings are equal it will increment a counter and get the next two string of 2 characters each until the main string is over but it is giving bogus data
eg: string : efef3030 should be ef023002 but its not please help
Code:
string comparision(string hextext) {
//string hextext = "";
int counter;
int length = hextext.size();
std::string twochars = "";
std::string secondtwochars = "";
trace() << "Input : " << hextext ;
string runlengthengthcoding = "";
/*for(int w=0;w<52;w++){
if(temphexholder[0] == databytes[w]){
counter++;
}
if(temphexholder[0] != databytes[w]){
runlengthengthcoding += databytes[w] + counter;
counter = 0;
temphexholder[0] = databytes[w];
}
} */
for(int i=0 ; i< length; i+=4)
{
twochars = hextext.substr(i, 2) ;
for(int k=2 ; k< length; k+=4)
{
secondtwochars = hextext.substr(k, 2) ;
if(twochars == secondtwochars){
counter++;
}
if(twochars != secondtwochars){
string counters = to_string(counter);
runlengthengthcoding += twochars + counters;
counter = 0;
}
}
}
return runlengthengthcoding;
}
int main (){
string t = "30304040";
cout << comparison(t);
}