Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Most efficient way to create a string range in C?

Most efficient way to create a string range in C?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12769904
    wrote on last edited by
    #1

    I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:

    a b

    for input 'a' , 'zz' , output is:

    a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz

    What is the most efficient way to do it in C?

    L J 3 Replies Last reply
    0
    • U User 12769904

      I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:

      a b

      for input 'a' , 'zz' , output is:

      a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz

      What is the most efficient way to do it in C?

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      There is no efficient way to do it you have to do a brute force roll of each character in each row. It's a standard odometer setup roll a column, when that is complete move the next column 1. Essentially it's just two functions roll character, roll next column a character. The roll next column will on occasion call itself because rolling one column needs to roll the next and next etc (watch an odometer on a car roll) As this is usually homework for a programming unit I will give you one of the functions, you need to do the other :-)

      void RollCharacter (char* MyStr, unsigned short Pos) {
      while (MyStr[Pos] <= 'z') {
      printf("Permutation string = %s\r\n", MyStr); // Print the entry
      MyStr[Pos]++; // Increment character
      }
      }

      //To test it
      char TestStr[10] = { 0 }; // Clear an array of character space
      TestStr[0] = 'c'; // Start start character . .. I choose "c"
      RollCharacter(TestStr, 0); // Roll that single character which is column zero

      The column roll is fractionally harder but not much.

      In vino veritas

      1 Reply Last reply
      0
      • U User 12769904

        I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:

        a b

        for input 'a' , 'zz' , output is:

        a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz

        What is the most efficient way to do it in C?

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        I should also say there is a generic form of counting used in encryption that uses sets of characters to numbers. It isn't fast to code because it requires loops with modulo and divides but it gives you a count in a set of restricted characters or characters in any order. Consider a set of characters abcABC and that was the digit order like 1,2,3,4,5,6 So counting 0 = a, 1 = b, 2 = c, 3 = A, 4 = B, 5= C, 6 = aa, 7 = ab, 8 = ac, 9 = ba etc The code to display a count sequence is done like this but I would not call it efficient :-)

        static char CodeSet[6] = { 'a', 'b', 'c', 'A', 'B', 'C'};

        void CovertCountToCode(char* Buf, unsigned short BufSize, unsigned long Count) {
        if ((Buf) && (BufSize)) { // Sanity check on buffer and size
        BOOL notComplete = TRUE; // Not completed conversion starts true
        unsigned short column = BufSize - 2; // We start right column for this
        memset(Buf, 0x20, BufSize); // Clear buffer to spaces
        Buf[BufSize - 1] = '\0'; // Terminate string
        do {
        unsigned short digit = Count % _countof(CodeSet); // Modulo to set size to get "digit"
        Buf[column] = CodeSet[digit]; // Transfer the character at set position
        column--; // Move a column left
        if (Count >= _countof(CodeSet)) { // If we will need to loop again
        Count /= _countof(CodeSet); // Divid by set size
        Count--; // Subtract one
        } else notComplete = FALSE; // Loop is now complete
        } while ((notComplete) && (column > 0)); // Loop until complete or out of buffer room
        }
        }

        void CountInCode(unsigned short MaxCount) {
        char Buf[10]; // Buffer for our display text to build
        unsigned short i;
        for (i = 0; i < MaxCount; i++) { // For each count
        CovertCountToCode(Buf, _countof(Buf), i); // Convert count to display text
        printf("Code string = %s\r\n", Buf); // Print the display text
        }
        }

        // Code to display first 50 counts .. which will be ... a to abb
        CountInCode(50);

        In vino veritas

        1 Reply Last reply
        0
        • U User 12769904

          I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:

          a b

          for input 'a' , 'zz' , output is:

          a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz

          What is the most efficient way to do it in C?

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          Efficient in terms of speed or lines of code? If the latter, create a string buffer and then manipulate it until it is greater than the end.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups