Results 1 to 6 of 6

Thread: need a little C++ help

  1. #1
    Junior Member
    Join Date
    Apr 2002
    Posts
    7

    need a little C++ help

    i need to write a function that will display all possible strings of lowercase letters, for instance..

    a
    b
    c
    d
    ...
    x
    y
    z
    aa
    ab
    ac
    ad
    ...


    and so forth (up to say, 5 characters). i'd really appreciate source code, but if someone can just point me in the right direction, i would be greatful.

    i know it can be done with loops, but i'd prefer a way which is a little more tactful.

    can this be done with recursion?

    anyway, i appreciate any help i can get.

  2. #2
    Banned
    Join Date
    Sep 2001
    Posts
    852
    umm if you cant figure out this with loops your not going to be able to do recursion lol
    its pretty hard calling functions within functions
    i have a source somewhere to do this if i cant find it ill write one up
    pm if you have any major questions
    RiOtEr

  3. #3
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    for now, here's one with loops (and yes, I know this isn't what you asked for, or to clean, but it may start you off):

    Code:
    #include <stdio.h>
    
    void PrintList();
    
    int main()
    {
    	int choice;
    	puts( "press 1 to run, 2 to exit" );
    	scanf("%d", &choice );
    	
    	if ( choice = 1 )
    		PrintList();
    
    	return 0;
    }
       	
    void PrintList()
    {	
       int c1, c2, c3, c4, c5; 
       char ch1, ch2, ch3, ch4, ch5;
       char alpha[28] = " abcdefghijklmnopqrstuvwxyz";
       
       c1=0;
       c2=0;
       c3=0;
       c4=0;
       c5=1;
       while ( c1 < 27 )
       {
    	   while ( c2 < 27 )
    	   {
    		   while ( c3 < 27 )
    		   {
    			   while ( c4 < 27 )
    			   {
    				   while ( c5 < 27 )
    				   {
    					   
    					   ch1 = alpha[c1];
    					   ch2 = alpha[c2];
    					   ch3 = alpha[c3];
    					   ch4 = alpha[c4];
    					   ch5 = alpha[c5];
    					   
    					   printf( "%c%c%c%c%c", ch1, ch2, ch3, ch4, ch5 );
    
    					   c5++;				   
    				   }
                       c4++;
    				   c5=1;
    			   }
    			   c3++;
    			   c4=1;
    		   }
    		   c2++;
    		   c3=1;
    	   }
    	   c1++;
    	   c2=1;
       }
    }
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    Ok here's something that I kludged together. Using recursion for this is a BAD idea. You'll run out of stack space really fast for a brute force algorithm like this. You can implement anything that uses recursion using iterative structures instead so that's what I recommend. The following code will do what you want. I used Borland C++ 5.0 since you didn't specify a compiler.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string num2base(int, int, int);
    
    int main() {
    
        // change these to use a different range of ascii chars
        char LOWER_BOUND = 'a';
        char UPPER_BOUND = 'z';
    
        // change this to vary the string length
        int STRING_LENGTH = 5;
    
        int TOTAL_PERMUTATIONS = pow((UPPER_BOUND - LOWER_BOUND + 1), STRING_LENGTH);
        string s;
    
        for (int i = 0; i < TOTAL_PERMUTATIONS; i++) {
            s = num2base(i, (UPPER_BOUND - LOWER_BOUND + 1), LOWER_BOUND);
            if (s == "") {
                cout << (char)LOWER_BOUND << endl;
            } else {
                cout << s << endl;
            }
        }
    }
    
    string num2base(int num, int base, int offset) {
    
        string s = "";
        while(num) {
            s = s + (char)((num % base) + offset);
            num = (num / base);
        }
        return s;
    }
    Hope this helps .
    OpenBSD - The proactively secure operating system.

  5. #5
    Junior Member
    Join Date
    Apr 2002
    Posts
    7
    THANKS SMIRC!!!

    that's perfect!
    exactly what i was looking for.

    btw, if anyone wants to use smirc's code, you might have to add:

    #include <math.h>

    for the power function. (i had to with visual c++)

  6. #6
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    No problem, glad I could help. Borland does most includes automatically so I didn't have to explicitly include the math.h library. I probably should have done this but (like I said) this was a very quick and dirty solution but it does the job .
    OpenBSD - The proactively secure operating system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •