Below is an example of a string and its scrambled form:
|Original string||Scrambled version|
An easy way to achieve this scrambling, although not a very good one for security purposes, is to swap pairs of characters in the string until a reasonable scramble is reached. For example, we could have achieved the above scramble with the following steps:
A run of this program might look like:
% scramble Enter a string to scramble (enter no spaces and no more than 80 characters). String? articulate Now enter pairs of numbers representing positions to swap to create the scrambled version (e.g., "1 3"). Positions are numbered between 1 and 10 for this string. Enter "0 0" when you are done. Swap? 1 2 New scrambled version: raticulate Swap? 2 3 New scrambled version: rtaiculate Swap? 0 8 Positions must be between 1 and 10! Swap? 10 7 New scrambled version: rtaicueatl Swap? 0 0 The string: articulate was scrambled to: rtaicueatl
Note that, to the user, positions in the string start with 1.
the core of such a string-scrambling program. Note the additional header
string.h) for string functions (like
And thus, has been declared in the program as:
Note that we must allow for one extra character, the nul character (
\0). The nul character signals the end of the string, since the string may not use the entire space in the array. Besides, library functions or objects that deal with strings will either put it in (e.g., when reading a string from the user) or expect it to be there (e.g., in a string being printed).
Because the user of the program will view the first position in the string as 1 (i.e., one):
positions given by the user will have to be translated to zero-based indices (i.e., array indices start with 0).
This alternate method will use the original string and a map that tells us how the positions have been permuted. So, accessing the string via the map will allow us to view the string in scrambled form. Initially, the map will refer to the string in unscrambled form...
Since this map is an array containing pointers to elements in
the string, map's elements must be of type pointer to character
You can read that as "an array with MAX_STR_LENGTH elements, where each element is a pointer to a character."char *map[MAX_STR_LENGTH];
void InitializeMap(char *map[MAX_STR_LENGTH], char str[MAX_STR_LENGTH+1], int length)
This function sets up the
map so that it refers to the string
in its initial unscrambled form.
void ScrambleMap(char *map[MAX_STR_LENGTH], int length)
This functions performs all the swaps that produce the scramble. Pairs
of positions to swaps are prompted for and read in from the user until
"0 0" is entered. Rather than changing the string, the
void PrintScrambled(char *map[MAX_STR_LENGTH], int length)
This function prints out the string using the scrambled order given by
map. Output should go to standard output.
ScrambleMap()set up all the pointers?
PrintScrambled()print out the scrambled version of the string--doesn't it need the string itself (i.e.,
str) as a parameter?