isogram.h
#ifndef ISOGRAM_H
#define ISOGRAM_H
#include <stdbool.h>
bool is_isogram(const char phrase[]);
#endif
isogram.c
#include "isogram.h"
#include <stdint.h>
bool is_isogram(const char phrase[])
{
if (!phrase)
return false;
uint32_t letter_flags = 0;
char sub = 'a';
while (*phrase) {
char letter = *phrase;
if (letter >= 'a' && letter <= 'z')
sub = 'a';
else if (letter >= 'A' && letter <= 'Z')
sub = 'A';
else
sub = 'X';
if (sub != 'X') {
if ((letter_flags & (1 << (letter - sub))) != 0)
return false;
else
letter_flags |= (1 << (letter - sub));
}
phrase++;
}
return true;
}
This approach uses the ASCII value of the letter to set the corresponding bit position.
The approach starts by checking if the pointer to the input phrase is NULL
.
If so, then the function returns false
.
There is a difference between a NULL
pointer and a valid pointer pointing to a null character ('\0'
).
The uint32_t
value for the bit field is initialized with 0
.
A character to subtract by is initialized to 'a'
.
The input phrase
is looped through its characters, looking for a character being a
through z
or A
through Z
.
- The ASCII value for
a
is97
, and forz
is122
. - The ASCII value for
A
is65
, and forZ
is90
.
The character to be subtracted by is set according to whether the character is a lowercase letter, uppercase letter, or something else. If a character is neither an uppercase or lowercase letter, it is ignored. Otherwise, the ASCII value of the letter is used to calculate its position in the bit field.
- If a lowercase letter is subtracted by
a
, thena
will result in0
, because97
minus97
equals0
.z
would result in25
, because122
minus97
equals25
. Soa
would have1
shifted left 0 places (so not shifted at all) andz
would have1
shifted left 25 places. - If an uppercase letter is subtracted by
A
, thenA
will result in0
, because65
minus65
equals0
.Z
would result in25
, because90
minus65
equals25
. SoA
would have1
shifted left 0 places (so not shifted at all) andZ
would have1
shifted left 25 places.
In that way, both a lowercase z
and an uppercase Z
can share the same position in the bit field.
So, for an unsigned thirty-two bit integer, if the values for a
and Z
were both set, the bits would look like
zyxwvutsrqponmlkjihgfedcba
00000010000000000000000000000001
The bitwise AND operator can be used to check if a bit has already been set.
If it has been set, the letter is duplicated and false
can be returned immediately.
If it has not been set, then the bitwise OR operator can be used to set the bit.
If the loop completes without finding a duplicate letter (and returning false
), the function returns true
.