C Counting Algorithm

This is a discussion on C Counting Algorithm within the C Programming forums, part of the General Programming Boards category; Hi, I've written the code below which prints every possible combination of the lowercase alphabet for three letters. I'm wondering ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    C Counting Algorithm

    Hi,

    I've written the code below which prints every possible combination of the lowercase alphabet for three letters. I'm wondering if anybody knows how I can change this code so it can work with a variable length array? I've been trying to work this out for a while now, it seems pretty tricky.

    Thanks.

    Code:
    #include <stdio.h>
    
    static const char *s = "abcdefghijklmnopqrstuvwxyz";
    
    static void printarr(const char *arr[], int len)
    {
          int i;
    
          for(i = 0; i < len; i++)
                printf("%c", *arr[i]);
          printf("\n");
    }
    
    int main()
    {
          const char *arr[3];
          
          for(arr[0] = s; *arr[0] != '\0'; arr[0]++) {
                for(arr[1] = s; *arr[1] != '\0'; arr[1]++) {
                      for(arr[2] = s; *arr[2] != '\0'; arr[2]++) {
                            printarr(arr, 3);
                      }
                }
          }
          return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    In the same way that adding one to
    "000009"
    gets you to
    "000010"

    You could also do
    "aaaaaz"
    becomes
    "aaaaba"

    Just prepare your char array with as many "a"'s as you need, then count up to 'z' at the right hand side, working any "carry" to the left.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    This is all you need for a VLA:
    Code:
        int len = 5;
        const char *arr[len];
    Now, in order to actually process that, since you can't dynamically change the number of nested loops in your code, you're going to have to make some sort of recursive function that prints everything out.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    A recursive function is no better than looping. It would have the same problems.

    Salem's solution works.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by King Mir View Post
    A recursive function is no better than looping. It would have the same problems.

    Salem's solution works.
    Yeah, you're right. Don't know what I was thinking...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. implementing an hours counting algorithm
    By droseman in forum C Programming
    Replies: 6
    Last Post: 02-04-2009, 02:59 AM
  2. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  3. Counting
    By rculley1970 in forum C Programming
    Replies: 8
    Last Post: 02-12-2006, 09:33 PM
  4. counting down...
    By twomers in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2005, 01:51 PM
  5. counting
    By threahdead in forum C Programming
    Replies: 0
    Last Post: 10-19-2002, 10:33 AM

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