I need an algorithm.... please help

This is a discussion on I need an algorithm.... please help within the C Programming forums, part of the General Programming Boards category; I need an algorithm who can create strings which are like a ... ... z aa .... ... za ab ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    35

    I need an algorithm.... please help

    I need an algorithm who can create strings which are like

    a
    ...
    ...
    z
    aa
    ....
    ...
    za
    ab
    ....
    ...
    zb
    ac
    ...
    zz
    aaa
    ....
    .....

    like a brute force.

    Do you know the name of this algorithm, so i can search through the net for an optimized version?

    I made such an algorithm, but i think it is slow, so i'm searhing for an optimized version.

    Please help me

  2. #2
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    How bout posting your code so we can give you some optimization tips?
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    I am rewriting it, I think tomorroe (3-15-2003) I'll post what I coded

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    Here it is:

    Code:
    	int i;
    //	char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char CharSet[] = "abc";
                    char string[50];
    	int PosCharSet[50]; // for every character of the string it is a "pointer" to the letter of the charset
    
    	min = 0;
    	max = sizeof (CharSet) - 2;
    
                    memset (&string, 0, sizeof (string));
    
    	for (i = 0; i < 50; i++)
    		PosCharSet[i] = 1;
    
    	for (;;)
    	{
    		for (i = min; i <= max; i++)
    		{
    			string[0] = CharSet[i];
                                                    printf (string);
    		}
    
    		i = 1;
    
    		while (string[i] == CharSet[max])
    		{
                                                    string[i] = CharSet[min];
    			PosCharSet[i] = 1;
    			i++;
    		}
    
    		if (string[i] == NULL)
    		{
    			string[i] = CharSet[min];
    			PosCharSet[i] = 1;
    		}
    		else
    		{
    			string[i] = CharSet[PosCharSet[i]];
    			PosCharSet[i]++;
    		}
    Tell me what you think about it, and if you know another way to make it, tell me, I don't want an optimized version of my algorithm only, if there is a different algorithm, which does what I need and it is faster, write it

    Thank you for your help, waste of time and patience

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Here's some code which may do what you want. Change MAX_LEN for the code length i.e. MAX_LEN 3 will give 'a' to 'zzz'
    Code:
    #include <stdio.h>
    
    int GetNextCode(char* c, int max)
    {
         for(int i = 0; i < max; i++)
         {
              if(!c[i])
              {
                   c[i] = 'a';     
                   return 1;
              }
    
              if( c[i] < 'z' )
              {
                   c[i]++;
                   return 1;
              }
              else
                   c[i] = 'a';     
         }
         return 0;     
    }
    
    #define MAX_LEN 3
    int main()
    {
         char code[MAX_LEN + 1] = {0};
    
         while( GetNextCode(code, MAX_LEN) )
              printf("%s ", code);
    
         return 0;
    }
    Last edited by Scarlet7; 03-15-2003 at 07:25 AM.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    hank you very much for your help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21