Isogram check

How about creating an array of bool [26] set to false, then for every character say a - ASCII 87 I think, minus 27 to give it 0.
Then everytime a character is ‘seen’ change from false to true in the array and check for that condition.

That would work as well, basic implementation:

bool IsIsogram(std::string Word)
{
	std::array<bool, 26> LettersSeen;
	LettersSeen.fill(false); //fill values with false
	for (char Letter : Word)
	{
		Letter = tolower(Letter);
		int Index = Letter - 'a'; //Index = 0 for 'a', 1 for 'b' etc.
		if (LettersSeen[Index])
		{
			return false;
		}
		else
		{
			LettersSeen[Index] = true;
		}
	}
	return true;
}

Would fail if anything but a value or a-z or A-Z is entered though, so would need to make sure alphabetic characters are in the string before passing it to this function.

Privacy & Terms