Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C - Shifting Strings


chess_rock's Avatar
Member
0 0

I was finishing my Huffman Code, and then i realized i was in real trouble. I'm trying to print characters that have not been compressed as simple char type, but when i need to print compressed characters, i need to print them bitwise. The fact is, if i throw everything into a string, i'm not able to shift the elements to the left or right, because of memory corruption errors.

The question is: "Is there any way to shift strings (preferrably dinamically allocated ones), to the left or right, without memory corruption errors?"

I've been thinking of using unions, but i have never used unions before. Also, I don't have a specific size of file, unless i use a very huge constant like:

    char my_big_array[ 9999999 ] ;
};```

ghost's Avatar
0 0
#include <stdio.h>
#include <string.h>

int
main( void )
{
    char string[13] = "Hello Kitty!";
    int   i, j,
          len       = strlen( string );

    for( i = 0 ; i < what_you_want ; ++i )
    {
        for( j = len-1 ; j > 0 ; --j )
            string[j] = string[j-1];
        *string = ' ';
    }

    printf( "%s", string );

    return( 0 );
}

chess_rock's Avatar
Member
0 0

I was talking about bitwise string shift, so that piece of code does not help me out.

I have solved my problem with the use of a really complex algorithm, which i believe, i'm the only person that understands it.


ghost's Avatar
0 0

chess_rock wrote: I have solved my problem with the use of a really complex algorithm, which i believe, i'm the only person that understands it. Wow, aren't we humble? By all means, do post your code anyway, might be interesting to some how you decided to solve your problem. Especially since you substituted a double linked list for a tree.


stealth-'s Avatar
Ninja Extreme
0 0

COM wrote: [quote]chess_rock wrote: I have solved my problem with the use of a really complex algorithm, which i believe, i'm the only person that understands it. Wow, aren't we humble? By all means, do post your code anyway, might be interesting to some how you decided to solve your problem. Especially since you substituted a double linked list for a tree.[/quote]

Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.


yours31f's Avatar
Retired
10 0

Complex does not always mean good.


ghost's Avatar
0 0

Ok, here's another one:

#include <stdio.h>
#include <string.h>

void
shift( char *str, int act, int carry, int times )
{
    static int size;
    int        bit;
    char       tmp;

    if( times <= 0 )
        return;

    if( act == 0 )
        size = strlen( str );

    bit      = str[act] & 1;                           /* the most right bit  */

    tmp      =   (str[act] >> 1)                       /* shift right and add */
               | (carry << (8 * sizeof( int ) - 1) );  /* the most left bit   */

    str[act] = tmp;

    if( ++act < size )
        shift( str, act, bit, times );             /* continue with the carry */

    if( --times > 0 )
        shift( str, 0,   0,   times );            /* start from the beginning */

    return;
}

int
main( void )
{
    char
        str[] = "Hello Kitty!";

    shift( str, 0, 0, 1);

    printf( "%s\n", str );

    return( 0 );
}

chess_rock's Avatar
Member
0 0

stealth- wrote: Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.

COM, stealth- is right. It is not by any means genius, but, confusing poor coding. I will still fix the code and make it look readable, for i haven't used many comments and some functions are ridiculously huge. Below you can see my poor coded functions, yet complex, to solve my shifting problem:

                   List * most ,
                   char caption ,
                   int * counter_bit ,
                   int * counter_byte )
{
  unsigned char temp ;
    
  int flag ;
  int i ;
  int how_many_bits ;
    
  List * runner ;
 
  flag = 0 ;
    
  for( runner = most ; runner!= NULL ; runner = runner->before )
  {
    if( runner->letter == caption )
    {
      flag = 1 ;
      break ;
    }
  }
  if( flag == 0 )
  {
    for( i = 0 ; i < 8 ; i++ )
    {
      temp = caption ;
      comp_file[ *counter_byte ] |= ( ( ( temp >> ( 7 - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;
 
      *counter_bit += 1 ;
      if( *counter_bit > 7 )
      {
    *counter_bit = 0 ;
    *counter_byte += 1 ;
      }
    }
  }
  else
  {
    temp = runner->amount ;
    for( i = 0 ; i < 8 ; i++ )
    {
      if( ( runner->amount >> i ) == 0 )
    break ;
    }
    how_many_bits = i - 1 ;
    
    temp = runner->amount ;    
    
    for( i = 0 ; i <= how_many_bits ; i++ )
    {
      comp_file[ *counter_byte ] |= ( ( ( temp >> ( how_many_bits - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;
      
      *counter_bit += 1 ;
      if( *counter_bit > 7 )
      {
    *counter_bit = 0 ;
    *counter_byte += 1 ;
      }
    }
  }
}```

If you pay close attention, you'll see the meaning of complex, yet not well done:

```markupcomp_file[ *counter_byte ] |= ( ( ( temp >> ( how_many_bits - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;

ghost's Avatar
0 0

chess_rock wrote: [quote]stealth- wrote: Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.

COM, stealth- is right.[/quote] Then sorry, my mistake.

I checked your code out and took the liberty of changing some things up. This ought to work as you intended. Try it, look through it or ignore it, whatever you prefer.

{
    bool flag;
    int i;

    List * runner ;
    (*counter_bit) %= 8;

    for( runner = most ; runner!= NULL ; runner = runner->before )
    {
        if( runner->letter == caption )
        {
            flag = true ;
            break ;
        }
    }
    if( !flag )
    {
        comp_file[*counter_byte] |= caption >> *counter_bit;
        comp_file[(*counter_byte)+1] |= caption << (8 - *counter_bit);
        (*counter_byte)++;
        return;
    }
    for( i = 0 ; i < 8 ; i++ )
    {
        if( ( runner->amount >> i ) == 0 )
            break ;
    }

    comp_file[*counter_byte] |= (runner->amount << (8 - i)) >> *counter_bit;
    if( i + (*counter_bit) > 7 )
    {
        (*counter_byte)++;
        comp_file[*counter_byte] |= runner->amount << i + (*counter_bit) - 8;
        (*counter_bit) -= 8;
    }
}