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 1090447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:22:27+00:00 2026-05-16T23:22:27+00:00

void print_combinations(const std::string &str) { int i, j, k; int len = str.length(); for

  • 0
void print_combinations(const std::string &str)
{
        int i, j, k;
        int len = str.length();

        for (i = 0; i < len - 2; i++)
        {
                for (j = i + 1; j < len - 1; j++)
                {
                        for (k = j + 1; k < len; k++)
                                // show combination
                                cout << str.at(i) << str.at(j) << str.at(k) << endl;

                }
        }
}

This function does what I want, but I want to make it recursive, so that it can create combinations of arbitrary length.

  • 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-16T23:22:28+00:00Added an answer on May 16, 2026 at 11:22 pm

    Here is a try:

    #include <iostream>
    #include <string.h> // for strlen
    #include <stdlib.h> // for atoi
    #include <sstream>
    void expand_combinations(const char *remaining_string, std::ostringstream& i, int remain_depth)
    {
        if(remain_depth==0)
        {
            std::cout << i.str() << std::endl;
            return;
        }
    
        for(int k=0; k < strlen(remaining_string); ++k)
        {
            std::ostringstream l;
            l << i.str();
            l << remaining_string[k];
            expand_combinations(remaining_string+k+1, l, remain_depth - 1);
        }
        return;
    }
    int main(int argc, char **argv)
    {
        std::ostringstream i;
        if(argc<3) return 1;
        expand_combinations(argv[1], i, atoi(argv[2]));
        return 0;
    }
    

    Output of ./test fjord 3:

    fjo
    fjr
    fjd
    for
    fod
    frd
    jor
    jod
    jrd
    ord
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
How I can do something like this in C++: void my_print(format_string) { vector<string> data;
How to print the bit representation of a string std::string = \x80; void print
void print(part *item, int part_count) { int i=0; for (i=0; i<part_count; i++) { printf(Item
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
When I write the following program: file 1: #include <stdio.h> int global; void print_global1()
Given a string and a number, this function will produce all possible combinations of
Consider the following example: class MyContainer { std::vector<void *> v; public: void Put(void *x)
class CBase { public: void print() { cout<<In base print func\n; }; }; class

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.