Hello,

I am having a problem with the output for my sub-string generator program.

input: abc
desired output: a, b, c, ab, bc, ac, abc

current output: abc, abcc, abcb, abcbc, abca, abcac, abcab, abcabc

I am sure the error is right in front of me, but I have been having alot of difficulty figuring out how to pass the input to the "find_combinations" function.

thanks!

#include <iostream>
#include <string>

using namespace std;

int count = 0;
int find_combinations(string sofar, string full_str){
    if(full_str.length() == 0 ){
        cout << sofar << endl;
        ++count;
    }   
    else{
        char temp_str = full_str[0];
        full_str = full_str.substr(1);
        find_combinations(sofar , full_str);
        sofar.push_back(temp_str);
        find_combinations(sofar , full_str);
    }
    return count;
}
int main(){
    cout << "Enter word for possible combinations: ";
    string user_input;
    cin >> user_input;
    string post_input;
    post_input = user_input;
    find_combinations(user_input, post_input);
}   

Recommended Answers

All 2 Replies

I am a little bit confused as to what output you want your program to have, or rather what it is supposed to represent. You don't really seem to want substrings as you also mention "ac" but you do not mention things like "cb". Would "cb" be the same as "bc" in your case?

Also (and especially if the answer to the last question is yes) how do you deal with strings like "aabbcc"?

At first sight I thought you wanted something like the following:

#include <iostream>
#include <cassert>

void print_permutations (std::string characters, const unsigned int characters_left, const unsigned int permutation_size);

int main(void)
{
    std::string characters = "abcd";

    for (unsigned int i = 1; i <= characters.length(); i++)
    {
        print_permutations(characters, i, i);
    }

    return 0;
}

void print_permutations (std::string characters, const unsigned int characters_left, const unsigned int permutation_size)
{
    assert (permutation_size <= characters.length() && characters_left <= permutation_size);

    // Recursive case
    if (characters_left > 0)
    {
        // The index the next character will be placed at.
        unsigned int current_index = permutation_size - characters_left;

        // Simply Go for every character.
        for (unsigned int i = current_index; i < characters.length(); i++)
        {
            // Swap the character at the current index with the new character
            std::swap(characters[current_index], characters[i]);

            // And solve the rest in recursion.
            print_permutations(characters, characters_left - 1, permutation_size);

            // Swap it back for the rest of the cases.
            std::swap(characters[current_index], characters[i]);
        }
    }
    // Base case
    else
    {
        // Just print the permutation. It may be shorter then the string to
        // Only print the characters of the permutation section we generated.
        std::cout << characters.substr(0, permutation_size) << std::endl;
    }
}

But this is not what you want right? It would output:

a
b
c
ab
ac
ba
bc
cb
ca
abc
acb
bac
bca
cba
cab

Please show which of these results is/are undesired, and more importantly: why.

// l1.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

void build(string str,size_t n);
void main()
{   
    string str;
    cout<<"please input some letters:"<<endl;
    cin>>str;
    size_t n=str.size();
    build(str,n);
}
void build(string str,size_t n)
{
    if(n==0)
    {
        cout<<str;
        cout<<endl;
    }
    else
    {
        build(str,n-1); 
        string newStr(str);
        newStr.erase(n-1,1);
        build(newStr,n-1);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.