Click here to Skip to main content
Click here to Skip to main content
Go to top

A simple string generator in C++

, 10 May 2004
Rate this:
Please Sign up or sign in to vote.
Article on a simple string generator

Introduction

When we search something such as an user account in server, we need to generate strings for checking (I call them seeds). And from the following code, you can get a sequential string. This is something like that: if you initialize your seed sting with 'ab', the following will give you ac,ad,...az,aaa,aab... over and over all of the possible combinations in ch[0x100] except '\0'. What you need to do is to choose a char collection, to say, who will join in the permutation; this is a repeated combination, if you want unrepeated, then you can Google for it, I had seen it once, but I can't remember it now.

Using the code

So if you need a repeated permutation, you can consider to use the following code snippet. Firstly, you choose char collection, and initialize it, here I use [a-z]. After that, you need to initialize the seed. then the code will enter cycle, to get out the next combination for you. the result string are kept in szitem. You can use it in your search string. e.g. you can make it to be "rcpt to:<" + szitem + "@maildomain.com>\r\n", and send out to the smtp server. If you want ANSI string, you can change it accordingly, here I use UNICODE. I don't think you still need me to do that Smile | :) anyway, if there is something needed, just drop me a line.

 #define ENUMCHARCOUNT 26 

 WCHAR ch[0x100]; 
 
 for(int i=0;i<ENUMCHARCOUNT;i++) 
  ch[i] = L'a'+i; 
 for(int i=ENUMCHARCOUNT;i<0x100;i++) 
  ch[i] = L'\0'; 
 
 // the maximum length of result string seeds is 10. 
 unsigned short seed[10]; 
 for(int j=0;j<10;j++) 
  seed[j] = 0xff; 
 
 WCHAR szitem[10]; 
 // initialize the seed here. so we will start from "ab".
 seed[0]= L'a';
 seed[1]= L'b';
 seed[2]= L'\0';
 while(1)
 {
  if(seed[0]==ENUMCHARCOUNT)
  {
   seed[0] = 0;
   int kk = 1;
   while(1) 
   {
    seed[kk]++; 
    if(seed[kk]==0x100) // upgrade
    {
     seed[kk] = 0;
     break;
    }
    else if(seed[kk]==ENUMCHARCOUNT) // flat add
    {
     seed[kk] = 0;
     kk++;
    }
    else
     break;
   }
  }
  
  j=0;
  while(1)
  {
   szitem[j] = ch[seed[j]];
   if(seed[j]==0xff)
     break;
   j++;
  }
  
  //
  // do sth with szitem
  //
  
  seed[0]++;
 }

Before you do cool things with the generated string seed, you can use regexps or other formula to filter it, if the checking process is time consuming (it always be).

History

Several days before, I used this to get out named items from a script engine Wink | ;-) Last what you should do with this code snippet, is cut out the above code, paste into your app source, and set a breakpoint at the last line, press F5, when it stop, drag sztiem into your watch panel. Then you can see what the cycle does for you. The last word what I should say is, every one who have been working on a search engine, he must have done the same thing as above, but I can't find out if anyone posted it here. So I did it myself. Really appreciate all of your comments! Happy weekend guys!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

ChauJohnthan
Software Developer (Senior)
China China
No Biography provided

Comments and Discussions

 
GeneralFor my documentation helper ! PinmemberKochise11-May-04 1:49 
GeneralRe: For my documentation helper ! PinmemberChauJohnthan11-May-04 7:00 
GeneralHey... PinmemberKochise11-May-04 21:09 
GeneralRe: Hey... PinmemberChauJohnthan12-May-04 7:39 
GeneralSeems I got the lowest score in CP:-))) PinmemberChauJohnthan7-May-04 16:09 
GeneralRe: Seems I got the lowest score in CP:-))) PinmemberJohn M. Drescher8-May-04 15:36 
GeneralRe: Seems I got the lowest score in CP:-))) PinsussAnonymous8-May-04 15:46 
GeneralA more elaborate explanation is needed. PinmemberChris Meech7-May-04 8:09 
GeneralRe: A more elaborate explanation is needed. PinmemberJohn M. Drescher7-May-04 10:41 
GeneralRe: A more elaborate explanation is needed. PinmemberChauJohnthan7-May-04 15:39 
GeneralRe: A more elaborate explanation is needed. PinmemberChauJohnthan7-May-04 15:29 
GeneralRe: Good luck guys ;-) PinsussAnonymous7-May-04 7:35 
GeneralRe: Good luck guys ;-) PinmemberChauJohnthan7-May-04 14:13 
GeneralRe: Good luck guys ;-) Pinmembermystro_AKA_kokie8-May-04 9:09 
GeneralRe: Good luck guys ;-) PinsussAnonymous8-May-04 15:43 
Generalwhat's this ... PinmemberMaximilien7-May-04 6:04 
GeneralRe: what's this ... PinmemberChauJohnthan7-May-04 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 11 May 2004
Article Copyright 2004 by ChauJohnthan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid