Hi all. I am trying to create a function to check if a string is a pangram or not. I think I have good start on it, but I can't figure out how to check if the characters in the string are unique. I have to do this recursively with no global or static variables. I created a counter variable the gets incremented every time the isPangram function gets called. I figure since there are 26 letters in the alphabet, if I can figure out how to insert 26 unique characters into a new string from the old string that would make it a pangram. Here is my code so far. Any pointers would be grateful.

#include <iostream>
#include <string>
using namespace std;

bool isPangram(string str, int counter, int last_index)
{
    if(counter >= 26)
    {
		return true;
	}
	else
	{
	    return isPangram(str, ++counter, --last_index);
	    //return false;
	}
}
bool isPangram(string str)
{
   return isPangram(str, 0, str.length() - 1);
}

Dani AI

Generated

Given the recursive-only constraint and the prohibition on global/static state, carry the “seen letters” state through the recursive calls. already has the right shape (a recursive wrapper); instead of external arrays or globals, a 26-bit integer can record which letters have been encountered (one bit per letter). The snippet below is a compact, safe, purely recursive solution that records uniqueness without extra global storage and returns true as soon as all 26 bits are set.

#include <string>
#include <cctype>

bool isPangram(const std::string &s, std::size_t pos = 0, unsigned mask = 0u) {
    const unsigned FULL = (1u << 26) - 1u;
    if (mask == FULL) return true;           // early success: all letters seen
    if (pos >= s.size()) return mask == FULL; // end of input

    int ch = static_cast<unsigned char>(s[pos]); // avoid UB for negative char
    ch = std::tolower(ch);
    if (ch >= 'a' && ch <= 'z') mask |= 1u << (ch - 'a');
    return isPangram(s, pos + 1, mask);
}

Explanation and notes: each lowercase letter sets one distinct bit; duplicates don’t change the mask so uniqueness is implicit. The code uses static_cast<unsigned char> before std::tolower to avoid undefined behavior on platforms where char is signed. Time complexity is O(n); extra memory is O(1) (the mask), but recursion consumes O(n) stack frames. The wrapper form (default params) allows calling isPangram(s) directly.

Troubleshooting and edge cases: for very long inputs prefer an iterative loop to avoid stack overflow. Accented or non-ASCII letters are not counted as their ASCII equivalents; for internationalized text apply normalization and locale-aware mapping before this test. This approach implements the per-letter tracking idea without globals and keeps the logic concise and easy to verify.

You could have 26 integers variables each assigned to a letter or a boolean array, whenever you parse one of the letters you change the appropriate integer to 1 and have a checking function to make sure that if any of the integers is above 1 you reject it and if your looping switch them all to 0 at the end.

I cannot use any global or static variables and it has to be done recursively.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.