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
 
renpy(6)							       Games								  renpy(6)

NAME
renpy - engine for creating visual novels SYNOPSIS
renpy [path to the script directory] DESCRIPTION
Ren'Py is a programming language and runtime, intended to ease the creation of visual-novel type games. It contains features that make it easy to display thoughts, dialogue, and menus; to display images to the user; to write game logic; and to support the saving and loading of games. Ren'Py tries to be like an executable script, allowing you to get a working game without much more effort than is required to type the game script into the computer. Ren'Py is implemented on top of python, and that python heritage shows through in many places. Many Ren'Py statements allow python expres- sions to be used, and there are also Ren'Py statements that allow for the execution of arbitrary python code. Many of the less-used fea- tures of Ren'Py are exposed to the user by way of python. By only requiring use of the simplest features of python, it's hoped that Ren'Py will be usable by all game authors. USAGE
If you run the program without any arguments, zou will get an interactive launcher from where you can select, run and work different projects. For running a script, you need to give the full path to the directory that contains the game you want to play. For example: renpy /usr/share/games/renpy/demo/ To learn how to use the game interface, you should install and play renpy-demo. FILES
The game data for each user is stored at ~/.renpy/ directory. The scripts can be installed in the system bz placing them under /usr/share/games/renpy/ , but you can run any script in an arbitrarz directory just by telling the path as the parameter to the game. SEE ALSO
You can find more information at http://www.renpy.org/ May 2007 renpy(6)
All times are GMT -4. The time now is 03:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy