isogram.c (891B)
1 #include <stdio.h> 2 #include "isogram.h" 3 4 char lowercase(char c) { 5 // Lowercase only ASCII uppercase chars. 6 if (c >= 65 && c <= 90) { return c + 32; } 7 return c; 8 } 9 10 bool is_symbol_char(char c) { 11 // Catch chars that precede or follow alpha chars. 12 if (c < 65 || c > 122) { return true; } 13 // Catch chars intervening the upper and lowercase alpha chars. 14 if (c > 90 && c < 97) { return true; } 15 return false; 16 } 17 18 bool is_isogram(const char phrase[]) { 19 if (phrase == NULL) { return false; } 20 // There are 26 lowercased ASCII alpha chars (97-122). 21 // We can use 26 indexes to keep track of each one. 22 char char_map[26] = { 0 }; 23 for (int i = 0; phrase[i] != '\0'; i++) { 24 if (is_symbol_char(phrase[i])) { continue; } // Ignore non-alpha chars. 25 if (char_map[lowercase(phrase[i]) - 97] != 0) { return false; } 26 char_map[lowercase(phrase[i]) - 97]++; 27 } 28 return true; 29 }