character combinations

This is a discussion on character combinations within the C Programming forums, part of the General Programming Boards category; Hello all. I need a function that produces this strings a , b, aa ,ab, ba ,bb ,aaa,aab, aba ,abb ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    character combinations

    Hello all.
    I need a function that produces this strings
    a , b, aa ,ab, ba ,bb ,aaa,aab, aba ,abb baa ,bab ,bba , bbb ,aaaa...
    It 's NOT exactly like binary counting (in binary after bbb (111) is aaaa (1000))

    char* GetNextString(char* previous)

    i.e . GetNextString("bba")="bbb" , GetNextString("bbb")="aaaa"
    I hope you understand what i need
    Thank you !

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I need a death ray. I need it to be able to wipe out entire armies and lots of buildings.

    How about you give me the death ray and I give you your GetNextString() function?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > It 's NOT exactly like binary counting
    Close enough though.
    Once you understand base 2 counting, plus I take it you already understand base 10 counting, then figuring out base 26 counting should be easy.
    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.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Salem View Post
    > It 's NOT exactly like binary counting
    Close enough though.
    Once you understand base 2 counting, plus I take it you already understand base 10 counting, then figuring out base 26 counting should be easy.
    This isn't base 26, he's using only a's and b's. And like he said, it's not the same as counting, since if we just say a is 0 and b is 1, then the increasing series would be:

    a, b, ba, bb, baa, bab, bba, bbb...

    That's not the series he wants.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    External loop is on string length
    Internal - is just a binary counter from 0 to 2^k-1
    for example for k = 3
    0 - 000 - aaa
    1 - 001 - aab
    2 - 010 - aba
    etc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by vart View Post
    External loop is on string length
    Internal - is just a binary counter from 0 to 2^k-1
    for example for k = 3
    0 - 000 - aaa
    1 - 001 - aab
    2 - 010 - aba
    etc
    But again, that's not the sequence he wants. What he wants is a lexicographic ordering of all strings of a's and b's. That is NOT the same as simple digital counting.

    EDIT: I'm stupid. Didn't fully comprehend what you wrote. Your method is right

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Wow MacGyver you must really want a death ray :|

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Quote Originally Posted by Salem View Post
    > It 's NOT exactly like binary counting
    Close enough though.
    Once you understand base 2 counting, plus I take it you already understand base 10 counting, then figuring out base 26 counting should be easy.
    Binary counting is not enaugh becauce has a 2^k limit (k=32 or 64) .I need combinations up to 1000 caracters(2^1000!!!).
    If this is easier i want this order 1-digit , 2-digit ..... n-digit (combinations )
    No problem if "bb" is before "ab" but a 5-digit combination cant be after a 6-digit combination.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I need combinations up to 1000 caracters
    So? What is the problem?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Quote Originally Posted by vart View Post
    So? What is the problem?
    I cant write : for(int i=0; i<2^500;i++)
    I think is better working with strings.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    char* incrementString(char* str)
    {
       size_t len = strlen(str);
       size_t index;
       for(index = len; index > 0; index--)
       {
          if(str[index-1] == 'a')
          {
             str[index-1] = 'b'; /* and exit */
             return str;
          }
          /* we have 'b' - increment it and go to the next char */
          str[index-1] = 'a';
       }
       /* all chars were 'b' - now they all are reset to 'a' 
        * return NULL pointer to indicate the end of iterations
        * calling function should append 'a' to the string and run the loop again
        * if the desired string length  is not achieved
        */
       return NULL;
    }
    Have not compiled or tested the code - but hope it shows the idea of incrementing arbitrary length string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character literals incorrectly interpreted
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 04-05-2009, 05:35 PM
  2. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  3. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 04:32 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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