Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:25:51+00:00 2026-05-28T20:25:51+00:00

C code to generate all strings of length upto k on a given set

  • 0

C code to generate all strings of length upto k on a given set of symbols. No symbol should be repeated in any string.
Example:
Take the 6 lower-case symbols in the English alphabet {a, b, c, d, e, f} and k = 3; print the first 25 strings of your output sorted in dictionary order.

Can anyone help me out please?

EDIT

# include <stdio.h>
# include <conio.h> 


void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}


void permute(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j));
       }
   }
} 


int main()
{
   char a[] = "ABC";
   permute(a, 0, 2);
   getchar();
   return 0;
}

This however will give the permutations of all the alphabets. Replacing the alphabets to be parametrized by blank space doesn’t seem to do the trick!
Even repetedly calling permute with permutable blank spaces doesnt sound efficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:25:52+00:00Added an answer on May 28, 2026 at 8:25 pm

    Assuming your pool of characters is sorted and without repetitions (may need some preprocessing), generating the desired strings in lexicographic order is automatic (in the approach I have in mind).

    Look at the short example of all nonempty strings that can be generated from “ABC” with the restrictions:

    A
    AB
    ABC
    AC
    ACB
    B
    BA
    BAC
    BC
    BCA
    C
    CA
    CAB
    CB
    CBA
    

    You need to keep track of

    1. How many characters you have chosen, which, and in which order: and int and a char[]
    2. How many characters you may still choose and which: an int and a bool[] (or char[], int[])
    3. How many strings you still need to output, since that will be modified in recursive calls: an int*

      void permute(char *pool, int pool_length, int num_picked, char *stringy,
      bool *picked, int max_length, int *strings_left) {

      int i;
      for(i = 0; *string_left > 0 && i < pool_length; ++i) {
          if (pool[i] may be picked) {
              // 1. pick that as the num_picked + 1st character
              // 2. output and decrement *strings_left
              // 3. recur
              // 4. unpick pool[i]
          }
      }
      

      }

    I hope that helps and I haven’t given too much away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to generate all possible strings from a given
Given a set of letters, say from A.. F, how can one generate a
For some reason, when I try to generate this code all at once, the
In http://www.anyexample.com/programming/java/java_simple_class_to_compute_md5_hash.xml an example is given how to calculate an MD5 hash of String.
I have some C# code that generates google maps. This codes looks at all
I have a bunch of PHP code that is auto-generated and it puts all
Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable
When I generate code using T4 templates in Visual Studio 2010, I get the
The following code will generate a link to the page I want to get
I use this code to generate a random number. Random R = new Random(0);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.