Complete this code for me so I can have a better understanding.
Decoding message in c
#define TRUE 1
#define FALSE 0
typedef struct StackStruct
{
int* darr; /* pointer to dynamic array */
int allocated; /* amount of space allocated */
int inUse; /* top of stack indicator
- counts how many values are on the stack */
} Stack;
void init (Stack* s)
{
s->allocated= 2;
s->darr = (int*) malloc ( sizeof (int) * s->allocated);
s->inUse = 0;
}
void push (Stack* s, int val)
{
/* add val onto stack */
s->darr[s->inUse] = val;
s->inUse = s->inUse + 1;
}
int isEmpty (Stack* s)
{
if ( s->inUse == 0)
return TRUE;
else
return FALSE;
}
int top (Stack* s)
{
return ( s->darr[s->inUse-1] );
}
void pop (Stack* s)
{
s->inUse = s->inUse - 1;
}
void reset (Stack* s)
{
/* how to make the stack empty? */
}
int main(int argc, char const *argv[])
{
char input[300];
/* set up an infinite loop */
while (1)
{
/* get line of input from standard input */
printf (" Enter input to check or q to quit ");
fgets(input, 300, stdin);
/* remove the newline character from the input */
int i = 0;
while (input[i] != ' ' && input[i] != '\0')
{
i++;
}
input[i] = '\0';
/* check if user enter q or Q to quit program */
if ( (strcmp (input, "q") == 0) || (strcmp (input, "Q") == 0) )
break;
printf ("%s ", input);
/*Start tokenizing the input into words separated by space
We use strtok() function from string.h*/
/*The tokenized words are added to an array of words*/
char delim[] = " ";
char *ptr = strtok(input, delim);
int j = 0 ;
char *wordList[15];
while (ptr != NULL)
{
wordList[j++] = ptr;
ptr = strtok(NULL, delim);
}
/*Run the algorithm to decode the message*/
}
printf (" Goodbye ");
return 0;
}
Decoding a Secret Message Alice and Bob are two spies that are under disguise in a hostile territory. They need to be in contact with each other regularly and share information about their mission. They cannot risk compromising their communication, so they decided to use encryption. Alice and Bob both have two alphabet lists, the first one is the alphabet they use for sending their messages and the second one is a dummy alphabet to make it harder to decode their messages. The main alphabet is L and the dummy alphabet is L2: L = a, b, c, d) L2 ={m, n, o, p) The letters from L are mapped to letters in L2. So {a} is mapped to {m}, {b} to {n}, {c} to {0}, and (d) to {p}. They send information using letters from L, but they also add letters from L2 to increase the complexity of encryption. Based on their communication rules, when either of them sends a message, it must contain words that have letters from L and L2. There are some twists: 1. The meaningful words in any message they broadcast are words that are "completely mapped". All the other words are meaningless. 2. Since L2 is mapped to L, they can use both set of letters interchangeably: so a string like "abnm is indeed completely mapped since "n" is mapped to "b" and "m" is mapped to "a". 3. Once you find the mapped words, you have to remove all letters from L2 to find the actual message (which is all in L) Example: Encrypted Message: abhcgmsopa bqcedpwon abmnpc abcdponm dfajbbmmn cabnmo" Decoded Message: "bcd abcd cab Your task is to write a C program that reads in an encrypted message and outputs the decoded message. You MUST use a stack implemented in a dynamic array. This dynamic array is to grow to a larger size when a push operation would be done to a full array causing an array overflow. For this program, your dynamic array MUST start with 4 positions in the array. When the array needs to grow, it size MUST grow by 4 additional positions each time (note the array to grow in size from 4 to 8 to 12 to 16 to 20 to ...). The push operation is now defined as follows: if (the stack array if full) grow the array add the value to the stack array increment the top-of-stack value The grow operation is defined as follows: Create a temporary pointer to the current stack array Allocate a new dynamic array of the larger size and Have the stack array variable refer/point to the new dynamic array Copy the existing values from the current stack array to the new dynamic array Deallocate the current stack array Update the maximum stack size variable Input The input for this program will come from standard input. Each line of input will be a series of string separated by space. You may assume that cach line of input is less than 300 characters long. The program must loop to read in multiple lines of input. If the input on the line contains only the letter q or Q. quit the program. Since we have limited the length of the input and are trying to process one line of input at a time, the best way to read the input is the fgets() function in the
library. Since we are reading from standard input, you are to use the value of stdin for the third parameter of fgets(). This causes fgcts to read input from the standard input. You MUST use fgets() for this programming project to read in the input. See the base code to see how this is done. If you are not familiar with the fgets functions, do a Google search and read. The cplusplus.com website has a good reference page on fgcts(). Stack Data Structure in the Algorithm For decoding the messages, we need to make sure every letter from L is perfectly masked by it's corresponding letter form L2. This mcans the sequence of occurrences are very important in decoding the message. Letter "a" is only valid when it's properly mapped by its substitution from L2 which is "m". We can also have nested maps which tricky to identify without a proper program. For checking perfect mapping, we will use a stack data structure implemented in a dynamic array. The stack must be empty at the start of the expression. Start by reading the string from left to right. If you see any letters from L, push them into the stack. The stack at every step will only hold letters from L. Once you encounter a letter from L2, compare it with the letter at the top of the stack. If the letter on the top of the stack is the perfect map for that letter, pop the stack and continue to the next letter. If the letter on the top of the stack does not map the letter from L2, the word is not part of the encrypted message and you can move to the next word. If the stack is empty, the word was perfectly map and part of our cncrypted message so you can add it to your decoded message. When the end of the word is encountered (i.e. the char null '\0'), check to see if the stack is empty. If the stack is empty, then the word was part of our encrypted message. If the stack is NOT empty, the word was not perfectly mapped and therefore meaningless. Decoding a Secret Message Alice and Bob are two spies that are under disguise in a hostile territory. They need to be in contact with each other regularly and share information about their mission. They cannot risk compromising their communication, so they decided to use encryption. Alice and Bob both have two alphabet lists, the first one is the alphabet they use for sending their messages and the second one is a dummy alphabet to make it harder to decode their messages. The main alphabet is L and the dummy alphabet is L2: L = a, b, c, d) L2 ={m, n, o, p) The letters from L are mapped to letters in L2. So {a} is mapped to {m}, {b} to {n}, {c} to {0}, and (d) to {p}. They send information using letters from L, but they also add letters from L2 to increase the complexity of encryption. Based on their communication rules, when either of them sends a message, it must contain words that have letters from L and L2. There are some twists: 1. The meaningful words in any message they broadcast are words that are "completely mapped". All the other words are meaningless. 2. Since L2 is mapped to L, they can use both set of letters interchangeably: so a string like "abnm is indeed completely mapped since "n" is mapped to "b" and "m" is mapped to "a". 3. Once you find the mapped words, you have to remove all letters from L2 to find the actual message (which is all in L) Example: Encrypted Message: abhcgmsopa bqcedpwon abmnpc abcdponm dfajbbmmn cabnmo" Decoded Message: "bcd abcd cab Your task is to write a C program that reads in an encrypted message and outputs the decoded message. You MUST use a stack implemented in a dynamic array. This dynamic array is to grow to a larger size when a push operation would be done to a full array causing an array overflow. For this program, your dynamic array MUST start with 4 positions in the array. When the array needs to grow, it size MUST grow by 4 additional positions each time (note the array to grow in size from 4 to 8 to 12 to 16 to 20 to ...). The push operation is now defined as follows: if (the stack array if full) grow the array add the value to the stack array increment the top-of-stack value The grow operation is defined as follows: Create a temporary pointer to the current stack array Allocate a new dynamic array of the larger size and Have the stack array variable refer/point to the new dynamic array Copy the existing values from the current stack array to the new dynamic array Deallocate the current stack array Update the maximum stack size variable Input The input for this program will come from standard input. Each line of input will be a series of string separated by space. You may assume that cach line of input is less than 300 characters long. The program must loop to read in multiple lines of input. If the input on the line contains only the letter q or Q. quit the program. Since we have limited the length of the input and are trying to process one line of input at a time, the best way to read the input is the fgets() function in the library. Since we are reading from standard input, you are to use the value of stdin for the third parameter of fgets(). This causes fgcts to read input from the standard input. You MUST use fgets() for this programming project to read in the input. See the base code to see how this is done. If you are not familiar with the fgets functions, do a Google search and read. The cplusplus.com website has a good reference page on fgcts(). Stack Data Structure in the Algorithm For decoding the messages, we need to make sure every letter from L is perfectly masked by it's corresponding letter form L2. This mcans the sequence of occurrences are very important in decoding the message. Letter "a" is only valid when it's properly mapped by its substitution from L2 which is "m". We can also have nested maps which tricky to identify without a proper program. For checking perfect mapping, we will use a stack data structure implemented in a dynamic array. The stack must be empty at the start of the expression. Start by reading the string from left to right. If you see any letters from L, push them into the stack. The stack at every step will only hold letters from L. Once you encounter a letter from L2, compare it with the letter at the top of the stack. If the letter on the top of the stack is the perfect map for that letter, pop the stack and continue to the next letter. If the letter on the top of the stack does not map the letter from L2, the word is not part of the encrypted message and you can move to the next word. If the stack is empty, the word was perfectly map and part of our cncrypted message so you can add it to your decoded message. When the end of the word is encountered (i.e. the char null '\0'), check to see if the stack is empty. If the stack is empty, then the word was part of our encrypted message. If the stack is NOT empty, the word was not perfectly mapped and therefore meaningless