In: Computer Science
C/ C++ Preferably
1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++) in others it's more difficult due to type casting restrictions (Java/Python). Make sure you can access both character and integer data at any location in the array.
In C/C++ think of the follow type of code:
int j, i, *ii;
char c, *cc;
char mem[MEM_SIZE];
ii = (int*)(mem + 10); //base address + 10
cc = mem+10;
2. Read data from a text file. Can't come up with your own test
data, cut and paste the words to 'Mary had a little lamb' into a
file or down load a text (.txt) version of the bible (big) from the
internet.
3. Use regular expressions, strtok or any command you want
including something you write yourself to break a line of text read
from a file into tokens. A token is just a sequence of characters
without a separator. The only separator you need is space ' ' but
if you want to use more like, comma ',', tab '\t' and semicolon ';'
go for it.
Answer 1
Code
#include <stdio.h>
#define MEM_SIZE 100
int main()
{
int i, *ii;
char *ch, c;
char arr[MEM_SIZE] = { 0 };
//writing value 243 in character array starting
from location arr+10
ii = (int*)(arr + 10);
*ii = 243678782; // 3e 3e 86 0e will be stored at
positions 10, 11, 12 and 13 respectively. LSB will be stored at
starting position and so on
// Retreiving value as character byte at location
arr+10
ch = (char*)(arr + 10);
c = *ch;
printf("Before changing value of integer starting
position 10 is: %d\n", *ii);
printf("value of c at position 10 is: %c\n", c); // c will get value as 3e which means character '>' (Ascii conversion)
// Now change the value at location arr+10 and
retrieve using integer
*ch = '8';
printf("Changed charater value at position 10 is: %c",
*ch);
printf("\nvalue of integer starting position 10 is:
%d", *ii); // New value will become 38 3e 86 0e => 243,678,776.
Position 10 byte is MSB.
return 0;
}
Test Output