You are here:

C/c storage minimal space

Advertisement


Question
Hi,
How can i do the following in C:
in order to store the string "abcd", i may need 5 bytes( 40 bits). What i want to do is store each element 'a' or 'b' or ... in its minimal strorage form. eg. if character 'a' is represented as 97 it may take roughly( i didn't do the math) 7 bits. And considering it takes 7 bits for the other chars i need 35 bits to store the string. How can i do this? Any creativity is acceptable. THANK YOU VERY MUCH!

Answer
Hi Heni,


If you see why we take 1 Byte (8 bits) space for storing a character is because ASCII characters range from 1 to 255 and hence we need 8 bits. as 255 is 11111111.

Now, question in hand is very specific, such that it can only have characters between "a-z" or "A-Z" in that case we can do alot of space reduction.

If this is the case in that case you can number them from 1 to 26 (a to z)
And (27-52) for A to Z.

And you can use 6 bits for storing them.

We can save more space if you have a condition say a character is not repeated.
In that case you only require max 52 bits that is only 6 characters.

Let me know the exact question after that we can see how much space we can save.

Regards,
Abhishek Kumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Abhishek Kumar

Expertise

I can answer questions related to basic concepts , arrays , expressions , pointers and queries related to coding .

Experience

I have done my bachelors in Computer Science. For past 5 years i have been working in c , and i have good command over most of the areas of C Language.

Education/Credentials
I did my Bachelors in Computer Science and Engineerin .

Awards and Honors

©2012 About.com, a part of The New York Times Company. All rights reserved.