Good job! Here's mine if you're interested.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define INPUTFILE "cipher.txt"
#define MAXWORD 3 // length of word "the"
#define PWORDLEN 3
int get_text(char *text) {
FILE *file = fopen(INPUTFILE, "r");
if (!file) { perror(INPUTFILE); exit(EXIT_FAILURE); }
int len = 0;
for (int ch; fscanf(file, "%d%*c", &ch) == 1; ) text[len++] = ch;
fclose(file);
return len;
}
int decrypt(const char *text, int len, const char *pword, char *out) {
while (len--) {
*out++ = *text++ ^ *pword++;
if (!isprint( out[-1]) && !isspace(out[-1])) return 0;
if (!*pword) pword -= PWORDLEN;
}
*out = 0;
return 1;
}
// Return words <= MAXWORD in length, converted to lowercase.
// A word is all letters with a non-letter before and after it.
const char *next_word(const char *pos, char *word) {
while (1) {
while (*pos && !isalpha(*pos)) ++pos;
if (!*pos) break;
const char *end = pos;
while (isalpha(*end)) ++end;
if (end - pos <= MAXWORD) {
for ( const char *p = pos; p < end; ++p )
word[p - pos] = tolower( *p );
word[end - pos] = 0;
return end;
}
pos = end;
}
return NULL;
}
int count_thes(const char *text) {
int cnt = 0;
char word[MAXWORD + 1];
const char *pos = text;
while ((pos = next_word(pos, word)))
cnt += strcmp(word, "the") == 0;
return cnt;
}
int main() {
char text[1500]; // Input has 1455 chars
int len = get_text(text);
for (char a = 'a'; a <= 'z'; ++a)
for (char b = 'a'; b <= 'z'; ++b)
for (char c = 'a'; c <= 'z'; ++c) {
char out[1500], pword[PWORDLEN + 1] = { a, b, c };
if (decrypt(text, len, pword, out) && count_thes(out) >= 5) {
printf("%s\n", pword);
printf("%s\n", out);
}
}
return 0;
}