Skip to content

Isogram

Intro

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

Task

Determine if a word or phrase is an isogram.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#if !defined(ISOGRAM_H)
#define ISOGRAM_H
#include "string"
#include "iostream"

namespace isogram {
    bool is_isogram(std::string test);
}  // namespace isogram

#endif // ISOGRAM_H
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include "isogram.h"
#include "iostream"
#include "string"
#include <cctype>
#include <vector>

namespace isogram {
bool is_isogram(const std::string test) {
  for (int i = 0; i < (int)test.size(); i++) {
    if (test[i] == ' ' || test[i] == '-')
      continue;
    for (int ii = i + 1; ii < (int)test.size(); ii++) {
      if (std::tolower(test[i]) == std::tolower(test[ii]))
        return false;
    }
  }
  return true;
}
} // namespace isogram

Last update: February 13, 2021

Comments