简体   繁体   中英

How to get every possible string of n characters in c++?

I know it is possible to use n nested for loops to get the result. This however isn't very flexible. If I wanted to get every string of n+2 characters I would have to write an extra two for loops.

I'm pretty sure I should use a parameter called n_Letters and use some kind of recursion. Any ideas? This is how my code looks right now. It gives all the 3 character combinations.

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

void StringMaker(){
   for(int firstLetter = 97; firstLetter < 123; firstLetter++){
    char a = firstLetter;
    for(int secondLetter = 97; secondLetter < 123; secondLetter++){
        char b = secondLetter;
        for(int thirdLetter = 97; thirdLetter < 123; thirdLetter++){
            char c = thirdLetter;
            cout << a << b << c << endl;
        }
    }
}
}

int main() {
    StringMaker(); // I could add a parameter n_Letters here
}

This is a simple tree traversal problem that can easily be solved using recursion. Using a counter ( count ) and accumulator ( partial ) recur on your function for each letter until count is zero then print partial .

#include <iostream>
#include <string>

void StringMaker(int count, std::string partial = "") {
    if (count == 0) {
        std::cout << partial << '\n';
    }
    else {
        for (char letter = 'a'; letter <= 'z'; ++letter) {
            StringMaker(count - 1, partial + letter);
        }
    }
}

int main() {
    StringMaker(3);
    return 0;
}

Edit: It seems their are some concerns with my answer regarding memory allocations. If it's a concern for you, consider this alternative solution. Increment the first character if it isn't 'z' , otherwise set it to a and repeat with the the second character. Do this until the last character is set from z to a . This acts as a sort of base 26 counter with count digits.

#include <iostream>
#include <string>

void StringMaker(size_t count) 
{
    std::string data(count, 'a');
    size_t i = 0;
    do
    {
        std::cout << data << '\n';

        for (i = 0; i < count; ++i)
        {
            auto & next_char = data[i];
            if (next_char < 'z') {
                ++next_char;
                break;
            }
            else {
                next_char = 'a';
            }
        }

    } while (i != count);
}

int main() {
    StringMaker(3);
    return 0;
}

Here is my just-for-fun solution:

void StringMaker(int n)
{
      int base = ('z' - 'a' + 1);
      std::string str(n, '\0');

      for(int i = 0; i < int_pow(base, n); ++i)
      {
            for(int j = 0; j < n; ++j)
            {
                  str[n - j - 1] = 'a' + i / int_pow(base, j) % base;
            }
            cout << str << '\n';
      }
}

Suppose we have i written in numerical system with base 26 (from a to z), so increment i with n = 4 give us aaaa, aaab and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM