Members Online
Total Online: 37 Web Spiders: 17
Guests Online: 34
Members Online: 3
Registered Members: 70170 Newest Member: bahmx
|
View Thread
| Author |
C - Shifting Strings |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
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:
union my_union{
char my_big_array[ 9999999 ] ;
};
|
|
| Author |
RE: C - Shifting Strings |
986
Member

Posts: 45
Location: Hungary
Joined: 18.01.08 Rank: Hacker Level 2 |
|
|
#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 );
}
Edited by 986 on 13-10-10 09:48 |
|
| Author |
RE: C - Shifting Strings |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
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. |
|
| Author |
RE: C - Shifting Strings |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
|
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.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: C - Shifting Strings |
stealth-
Member

Posts: 995
Location: Eh?
Joined: 10.04.09 Rank: God |
|
COM wrote:
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.
Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com |
|
| Author |
RE: C - Shifting Strings |
yours31f
Second to one

Posts: 1678
Location: Dallas Texas
Joined: 27.04.07 Rank: Satan |
|
Complex does not always mean good.
Debugging is what programmers do to beta software to make it take up more room on your hard drive if it is running too efficiently.

|
|
| Author |
RE: C - Shifting Strings |
986
Member

Posts: 45
Location: Hungary
Joined: 18.01.08 Rank: Hacker Level 2 |
|
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 );
}
|
|
| Author |
RE: C - Shifting Strings |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
|
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:
void FLE_store_all_compressed( char * comp_file ,
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:
comp_file[ *counter_byte ] |= ( ( ( temp >> ( how_many_bits - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;
|
|
| Author |
RE: C - Shifting Strings |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
chess_rock wrote:
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.
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.
void FLE_store_all_compressed( char * comp_file , List * most , unsigned char caption , int * counter_bit , int * counter_byte )
{
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;
}
}
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
|
|
|