c++ check if string is isogram

To check if a string is an isogram in C++, you can follow these steps:

  1. Declare a function named isIsogram that takes a std::string as input and returns a boolean value.

  2. Inside the function, create a std::unordered_set<char> named seenChars. This set will be used to keep track of the characters we have seen so far in the string.

  3. Iterate over each character in the input string using a range-based for loop. For each character, do the following:

  4. Convert the character to lowercase using the std::tolower function from the <cctype> header. This step is optional if you want to consider both uppercase and lowercase letters as the same.

  5. Check if the character is already present in the seenChars set. If it is, return false because it means the string is not an isogram. Otherwise, add the character to the seenChars set.

  6. After the loop, if we haven't returned false yet, it means that no duplicate characters were found in the string. In this case, return true to indicate that the string is an isogram.

Here is an example implementation of the isIsogram function:

#include <unordered_set>
#include <cctype>

bool isIsogram(const std::string& str) {
    std::unordered_set<char> seenChars;

    for (char c : str) {
        c = std::tolower(c);

        if (seenChars.count(c) > 0) {
            return false;
        }

        seenChars.insert(c);
    }

    return true;
}

You can use this function to check if a given string is an isogram by calling isIsogram(str), where str is the input string you want to check. The function will return true if the string is an isogram, and false otherwise.