Sponsored Content
Full Discussion: C++ help
Top Forums Programming C++ help Post 302466121 by Corona688 on Monday 25th of October 2010 02:06:08 PM
Old 10-25-2010
A quick update since I slightly misread your original problem. I had it generate strings of 1-5 characters by putting outlen in a for-loop.

Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
	// String of what letters can be used for input
	unsigned char *charset=	"abcdefghijklmnopqrstuvwxyz";
	// Might as well count combinations while we're at it
	unsigned long int loopcount=0;
	// We need this in order to shrink the list efficiently
	const int maxpos=strlen(charset);
	// How long an output string do we want?
	// can be a maximum of 16 in this implementation)
	int outlen;


	for(outlen=1; outlen<=5; outlen++)
	{
		// Set this to 0 to stop the main loop
		int running=1;
		// What letters have already been used.
		// Every time outlen changes, we blank it to all zeros.
		int chosen[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

		while(running)
		{
			// Just a counter for loops.
			int pos;	
			// A blank C-string, the current set of allowed characters.
			char ltrs[64];
			// A blank C-string, the current output string.
			char out[17];

			// Fill the output set with a-z.
			strcpy(ltrs, charset);

			loopcount++;	// Count another loop.

			// Generate the output string char by char.
			// out[0]=x, out[1]=y, ...out[outlen-1]=q,
			// out[outlen]='\0'
			for(pos=0; pos<outlen; pos++)
			{
				// Add the chosen letter to the output string
				out[pos]=ltrs[ chosen[pos] ];

				// Remove the character from the input set.
				// We do this by replacing it by the
				// char at the end, and shrinking by one.
				ltrs[ chosen[pos] ] = ltrs[ maxpos-(pos+1) ];
				ltrs[ maxpos-(pos+1) ] = '\0';
			}

			out[outlen]='\0';// NULL-terminate it so we can print it

			printf("%s\n", out);

			// Advance to the next combination.
			for(pos=0; pos<outlen; pos++)
			{
				// Pick one letter ahead.
				chosen[pos]++;

				// Are we beyond the max?  letter 0 can choose
				// 0-25, letter 1 can choose 0-24, etc, etc.
				if(chosen[pos] >= (maxpos - pos))
				{
					// If we are, reset it to 0 and advance
					// the next chosen character.
					chosen[pos]=0;

					// Out of digits!  We are finished.
					if(pos == (outlen - 1))
					{
						running=0;
						break;
					}
					else
					{
						// We must advance the next
						// digit.  while loop keeps going
						continue;
					}
				}
				// No overflowing digits?  We're done.
				break;
			}
		}
	}

	fprintf(stderr, "%lu combinations\n", loopcount);
	return(0);
}

The result is 8,268,676 combinations, which doesn't take much longer at all to do.

Last edited by Corona688; 10-25-2010 at 03:10 PM.. Reason: removed redundant variable
 
POLISH(5)							      Debian								 POLISH(5)

NAME
polish - a list of Polish words DESCRIPTION
/usr/share/dict/polish is an ASCII file which contains an alphabetic list of words, one per line. FILES
/etc/dictionaries-common/words is a symbolic link to a /usr/share/dict/<language> file. /usr/share/dict/words is a symbolic link to /etc/dictionaries-common/words, and is the name by which other software should refer to the system word list. See select-default-wordlist(8) for more information. The directory /usr/share/dict can contain word lists for many languages, with name of the language in English, e.g., /usr/share/dict/french and /usr/share/dict/danish contain respectively lists of French and Danish words if they exist. Such lists should be coded using the UTF-8 character set encoding. SEE ALSO
ispell(1), select-default-wordlist(8), and the Filesystem Hierarchy Standard. HISTORY
The words lists are not specific, and may be generated from any number of sources. The system word list used to be /usr/dict/words. For compatibility, software should check that location if /usr/share/dict/words does not exist. AUTHOR
Word lists are collected and maintained by various authors. Debian Project March 29th, 2011 POLISH(5)
All times are GMT -4. The time now is 05:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy