Tuesday, July 28, 2015

Pangram


interesting interview question
write a function to determine if a string is a pangram
pangram is defined as a string that has all the alphabets A to Z
it does not matter whether the characters are lower case or upper case

My Solution
Create an array of 26 booleans - one for each alphabet
set it to true each time you detect the alphabet
check at the end

bool IsPangram(string s)
{
bool flag[26];
int temp;

for (int index = 0; index < 26; index++)
{
flag[index] = false;
}

    for (int index = 0; index < s.length(); index++)
    {
temp = (int)tolower(s[index]) - 97;
flag[temp] = true;
printf("%d", tolower(s[index]));
    }
printf("\n");

for (int index = 0; index < 26; index++)
{
if (flag[index]==false)
{
return false;
}
}

return true;
}

One of the discoveries I made was that C++ standard library had an array container. It would simplify the initialization
  std::array<bool,26> myarray;

  myarray.fill(false);


No comments:

Post a Comment