AW: [Dev-C++] Producing a endless list of strings.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: manilius <man...@gm...> - 2003-07-30 21:07:26
|
Hi,
> Eventually I hope to use this to create strings at least
> 100 chars long (possibly 1000). Could someone tell me if
> 100 levels using recursion cause a stack overflow?
Do I understand you correctly that you want to create a list with every
possible string of 100 or even 1000 characters?
Even a list of all lower case strings with 10 letters takes about 10^15
bytes. You will definitely get a stack overflow :)
But here is how you could do it:
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
vector<char> CharacterTable;
void CreateAllPossibleStrings(string String, int RecursionLevel)
{
if(RecursionLevel > 0)
{
for(int i=0; i<CharacterTable.size(); i++)
CreateAllPossibleStrings(String+CharacterTable[i],RecursionLevel-1);
}
cout << String << endl;
}
int main(int argc, char *argv[])
{
// Init character table, only lower case letters in this case
for( int i=0; i<26; i++) CharacterTable.push_back('a'+i);
// Create all strings, recursion depth is 2...try to increase it :)
CreateAllPossibleStrings("",2);
system("PAUSE");
return 0;
}
ciao...
...manilius
|