Challenge
Conceal a binary code within a word.
Specifications
- The first argument is a path to a file.
- The file contains multiple lines.
- Each line is a test case represented by a space separated word and a binary code.
- For each test case, print the binary masked word.
- The mask alters the word according to the following:
- If the bit is 1, change the letter to upper case.
- If the bit is 0, leave the letter as is.
Constraints
- Words are from 1 to 20 letters long.
- The length of each word is equal to the length of the binary code.
- Words are always in lower case.
- The number of test cases is 40.
Sample Input
hello 11001
world 10000
cba 111
Sample Output
HEllO
World
CBA
My Solution
#include <stdio.h>
#include <ctype.h>
#define LINE_BUFFER 42
int main(int argc, char *args[]) {
if (argc < 2) {
puts("File path not provided.");
return 1;
}
if (argc > 2) {
puts("Excessive arguments, only the first will be considered.");
}
FILE *file = fopen(args[1], "r");
if (file == NULL) {
puts("Could not access file / file not found.");
return 1;
}
char line[LINE_BUFFER];
while (fgets(line, LINE_BUFFER, file)) {
for (int i = 0, word_length = 0; ; i++, word_length++) {
if (line[i] == ' ') {
line[i++] = '\0';
for (int j = 0; j < word_length; j++, i++) {
if (line[i] == '1') {
line[j] = toupper(line[j]);
}
}
printf("%s\n", line);
break;
}
}
}
fclose(file);
}