Created
October 28, 2015 14:32
-
-
Save carneeki/775463563671d4a5be9e to your computer and use it in GitHub Desktop.
Scan dictionary for words that have letters adding up to 100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE // F U RMS... won't compile without this! | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* Inspired by a silly meme that tries to connect the alphabet and corresponding | |
* letters with numbers (A=1, B=2, ... Z=26) such that "HARDWORK" and "KNOWLEDGE" | |
* are insufficient, and only "ATTITUDE" = 100%. | |
*/ | |
int sum(char*); | |
int main() | |
{ | |
char* word = (char *)malloc(sizeof(char) * 255); // 255 is plenty for dictionary words | |
size_t len = 0; // not actually used | |
ssize_t read; | |
FILE* fp = fopen("/usr/share/dict/words", "r"); | |
if(fp == NULL) | |
exit(EXIT_FAILURE); | |
while((read = getline(&word, &len, fp)) != -1) | |
if(sum(word) == 100) | |
printf("%s", word); | |
} | |
/* | |
* return the sum of the letters | |
*/ | |
int sum(char* str) | |
{ | |
int ret = 0; | |
for(int i = 0; i <=strlen(str); i++ ) | |
{ | |
if(str[i] >=65 && str[i] <= 90) | |
ret += (str[i] - 64); | |
else if(str[i] >= 97 && str[i] <= 122) | |
ret += (str[i] - 96); | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment