Reformat and document c/isogram
This commit is contained in:
+8
-11
@@ -3,14 +3,12 @@
|
|||||||
|
|
||||||
int length(const char string[]) {
|
int length(const char string[]) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (string[i] != '\0') {
|
while (string[i] != '\0') { i++; }
|
||||||
i++;
|
return i+1; // Account for terminating null char.
|
||||||
}
|
|
||||||
return i+1; // Account for terminating null character.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char lowercase(char c) {
|
char lowercase(char c) {
|
||||||
// Lowercase only ASCII capital characters.
|
// Lowercase only ASCII uppercase chars.
|
||||||
if (c >= 65 && c <= 90) {
|
if (c >= 65 && c <= 90) {
|
||||||
return c + 32;
|
return c + 32;
|
||||||
}
|
}
|
||||||
@@ -18,11 +16,11 @@ char lowercase(char c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool is_symbol_char(char c) {
|
bool is_symbol_char(char c) {
|
||||||
// Ignore symbols that precede alpha chars.
|
// Catch chars that precede or follow alpha chars.
|
||||||
if (c < 65 || c > 122) {
|
if (c < 65 || c > 122) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Ignore symbols intervening the upper and lowercase chars.
|
// Catch chars intervening the upper and lowercase alpha chars.
|
||||||
if (c > 90 && c < 97) {
|
if (c > 90 && c < 97) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -32,14 +30,13 @@ bool is_symbol_char(char c) {
|
|||||||
bool is_isogram(const char phrase[]) {
|
bool is_isogram(const char phrase[]) {
|
||||||
if (phrase == NULL) { return false; }
|
if (phrase == NULL) { return false; }
|
||||||
if (length(phrase) == 0) { return true; }
|
if (length(phrase) == 0) { return true; }
|
||||||
// There are 26 lowercased ASCII alpha chars.
|
// There are 26 lowercased ASCII alpha chars (97-122).
|
||||||
// We can use 26 indexes to keep track of each one.
|
// We can use 26 indexes to keep track of each one.
|
||||||
char char_map[26] = { 0 };
|
char char_map[26] = { 0 };
|
||||||
for (int i = 0; i < length(phrase); i++) {
|
for (int i = 0; i < length(phrase); i++) {
|
||||||
if (is_symbol_char(phrase[i])) { continue; } // Ignore non-alpha chars.
|
if (is_symbol_char(phrase[i])) { continue; } // Ignore non-alpha chars.
|
||||||
if (char_map[lowercase(phrase[i]) - 97] != 0) {
|
if (char_map[lowercase(phrase[i]) - 97] != 0) { return false; }
|
||||||
return false;
|
// Keep track of chars by incrementing the index value.
|
||||||
}
|
|
||||||
char_map[lowercase(phrase[i]) - 97]++;
|
char_map[lowercase(phrase[i]) - 97]++;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user