Thread: comparing characters in a string

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    5

    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); 
    }

  2. #2
    Guest
    Guest
    Don't just move on from prior threads. People are asking questions and giving advice to you. Start by adopting a proper indent style and pasting the whole code with your #includes.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Learn to indent code. Pick a style and stick to it.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string comparision(string hextext)
    {
      //string hextext = "";
      int counter = 0;
      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 << comparision(t);
    }
    See how much nicer this looks.

    2. Even if it looks pretty in your IDE, do NOT mix spaces and tabs for indentation (use spaces only). Sooner or later, something other than your editor will render all the tabs as dog food.

    3. Test before you post!.
    Code:
    foo.cpp: In function ‘int main()’:
    foo.cpp:46:23: error: ‘comparison’ was not declared in this scope
       cout << comparison(t);
    This just makes it look like you typed it from memory.

    4. Snipping out a couple of lines of includes does nothing but frustrate people who might otherwise bother to help. Your ideal should be that we should be able to copy/paste/compile/test with minimal hassle. If we have to start patching up your code just to make it work, your help rate will fall off a cliff.

    5. Learn to use a debugger.
    30 seconds with a debugger would have shown you just what the problem was.
    Code:
    $ g++ -g -std=c++11 foo.cpp
    $ ./a.out 
    30-1111830727400$ 
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b 33
    Breakpoint 1 at 0x4018fe: file foo.cpp, line 33.
    (gdb) run
    Starting program: a.out 
    
    Breakpoint 1, comparision (hextext="30304040") at foo.cpp:33
    33	        string counters = to_string(counter);
    (gdb) print counter
    $1 = -8711
    The reason - uninitialised variables.

    Now you know you can breakpoint, step and print, you should try and debug the next problem yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2018
    Posts
    5
    thanks guys for the assist

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional string arrays and comparing characters
    By ArcadeEdge in forum C Programming
    Replies: 4
    Last Post: 02-15-2012, 05:37 PM
  2. Comparing string for blank characters - C programming
    By dannydb368 in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 10:14 PM
  3. Comparing characters
    By Banana Man in forum C++ Programming
    Replies: 28
    Last Post: 01-13-2008, 11:11 PM
  4. Comparing Characters
    By luckygold6 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2003, 08:19 PM
  5. Comparing characters.
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2001, 09:50 AM

Tags for this Thread