Add c/isogram
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int length(const char string[])
|
||||
{
|
||||
int i = 0;
|
||||
while (string[i] != '\0') {
|
||||
i++;
|
||||
}
|
||||
return i+1; // Account for terminating null character.
|
||||
}
|
||||
|
||||
void copy_phrase(const char phrase[], char copy[]) {
|
||||
int i;
|
||||
for (i = 0; i < length(phrase); i++) {
|
||||
copy[i] = phrase[i];
|
||||
}
|
||||
copy[i+1]= '\0';
|
||||
}
|
||||
|
||||
char lowercase(char* c) {
|
||||
// Lowercase only ASCII capital characters.
|
||||
if (*c >= 65 && *c <= 90) {
|
||||
return *c + 32;
|
||||
}
|
||||
return *c;
|
||||
}
|
||||
|
||||
void lowercase_phrase(char phrase[]) {
|
||||
for (int i = 0; i < length(phrase); i++) {
|
||||
phrase[i] = lowercase(&phrase[i]);
|
||||
}
|
||||
}
|
||||
|
||||
bool is_symbol_char(char c) {
|
||||
// Ignore symbols that precede alpha chars.
|
||||
if (c < 65 || c > 122) {
|
||||
return true;
|
||||
}
|
||||
// Ignore symbols intervening the upper and lowercase chars.
|
||||
if (c > 90 && c < 97) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_isogram(const char phrase[]) {
|
||||
if (phrase == NULL) { return false; }
|
||||
if (length(phrase) == 0) { return true; }
|
||||
// Lowercase the phrase.
|
||||
// We make a copy, since the input is constant.
|
||||
char lower_phrase[(length(phrase))];
|
||||
copy_phrase(phrase, lower_phrase);
|
||||
lowercase_phrase(lower_phrase);
|
||||
// Check if the lowercased phrase is an isogram.
|
||||
for (int i = 0; i < length(lower_phrase); i++) {
|
||||
if (is_symbol_char(lower_phrase[i])) { continue; } // Ignore non-alpha chars.
|
||||
for (int j = 0; j < length(lower_phrase); j++) {
|
||||
if (is_symbol_char(lower_phrase[j])) { continue; }
|
||||
if (j == i) { continue; }
|
||||
if (lower_phrase[i] == lower_phrase[j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user