#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_SIZE 32
static int riskOverFlow(char* msg) {
return (sizeof(char)*(strlen(msg)+strlen("'\0'")+2) > sizeof(msg)) ? 1 : 0;
}
int main (int argc, char **argv) {
char *msg = malloc(INITIAL_SIZE*sizeof(char));
if (msg == NULL) {
fprintf(stderr, "ERROR: Failed to create dynamic string.\n");
exit(EXIT_FAILURE);
}
msg[0] = '\0';
for (int i = 1; i < argc; i++) {
if (riskOverFlow(msg)) {
char* tmp = realloc(msg, sizeof(msg)*2);
if (tmp == NULL) {
fprintf(stderr,"ERROR: Failed to increase length of dynamic string.\n");
exit(EXIT_FAILURE);
}
msg = tmp;
}
strncat(msg, argv[i], 2);
}
printf("The decoded message is %s\n", msg);
free(msg);
return EXIT_SUCCESS;
}
// Yeah, I'm too lazy to do things the file-handling way because I don't learn that until next sem.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv) {
for (int i = 1; i < argc; i += 2)
printf("%s ", argv[i]);
return EXIT_SUCCESS;
}
BEGIN [u]OperationDecodeMessage(string input) [/u]
SET count to 0
SET tally to 0
SET decodedString to “”
WHILE (count < length of input) DO
IF (input[count] == “ “) THEN
SET secondaryCount to count + 1
WHILE (secondaryCount < length of input) DO
SET newString to newString + input[secondaryCount]
INCREMENT secondaryCount by 1
END WHILE
OperationDecodeMessage(newString)
ELSE
IF (tally != 2) THEN
SET decodedString to decodedString + input[count]
INCREMENT tally by 1
END IF
END IF
INCREMENT count by 1
END WHILE
[u]END OperationDecodeMessage[/u]
BEGIN [u]Alcon5[/u]()
// Main question
SET max TO 256 // buffer to avoid overflow
GET string FROM user input
DECLARE Integer Index, Index2, Counter
SET Index, Index2, Counter TO 0
DECLARE char words[max][max]
SET words[max][max] TO NULL
// Iterate through entire string until null terminator
WHILE Index < length of string DO
IF string[Index] == ‘ ‘ THEN
Increment Counter
SET Index2 TO 0
END IF
ELSE
SET words[Index2][Counter] TO string[Index]
Increment Index2
END ELSE
Increment Index
END WHILE
SET Index, Index2 TO 0
WHILE Index < Counter DO
// while we still have more characters
WHILE words[Index2][Index] NOT NULL DO
IF Index2 NOT 0 OR Index2 NOT 1 THEN
// Challenge
words[Index2][Index] = NULL
END IF
ELSE
DISPLAY words[Index2][Index]
END ELSE
Increment Index2
END WHILE
Increment Index
Index2 = 0
END WHILE
END [u]Alcon5[/u]