permute string program

Thread Starter

nanobyte

Joined May 26, 2004
120
I'm trying to create a function with c++ that will show the permutes of a string of 3 chars. If I input a string with 3 unique chars the output is right for the first 3 permutes, but the last 3 are permutes that don't have all 3 chars. Instead the last 3 permutes are strings where one of the chars is repeated like below:

Enter a string: cat
cat
act
atc
att
att
tat

The code of the function is below. Could someone point me in the right direction?

Rich (BB code):
void permute_string(){
    char swap1;
    char swap2;
    int a=0;    //Points to 1st char being swapped
    int b=1;    //Points to 2nd char being swapped
    int n=3;
    int swap_count=0;    //keeps tracks position changes a char in has done
    int char_count=0;    //keeps tracks of char in the original string is being used
    char givenchars[3];
    char acopy[3];   // a duplicate of the original user input

    cout<<"Enter a string: ";
    for(int i=0; i<3; i++){
        cin>>givenchars; //Store original string input into array.
        acopy = givenchars; //copies original string input into a backup

    }

    while(char_count<3){
            cout<<givenchars[0]<<givenchars[1]<<givenchars[2]<<endl;

            swap1=givenchars[a];
            swap2=givenchars;

            //switch order
            givenchars[a]=swap2;
            givenchars=swap1;


            if(swap_count<n){
                swap_count++;
                a++;
                b++;
            }
            else{
                   n--;
                a=0;
                b=1;
                char_count++;
            }

    }



}
 

MrChips

Joined Oct 2, 2009
30,824
Your code attempts to switch characters at index a and b.
1st time a = 0, b = 1.
2nd time a = 1, b = 2.
3rd time a = 2, b = 3.
But the character at b = 3 has not been defined.
 

WBahn

Joined Mar 31, 2012
30,086
One of the most effective ways to debug code is to pretend that you are the processor and execute the program as it is written. Take a piece of paper and list the variables on it and keep track of the values in each variable at each step. Just be sure to do exactly what the instructions say and not what you wanted them to say.
 

Thread Starter

nanobyte

Joined May 26, 2004
120
The permutation of n is n! Where permutations of n = n(n-1)(n-2).... For a string 2 or 3 chars long the function works fine. When the string is > 3 the function doesn't give all the permutations, only the product of the first two integers. An example output is below. To avoid confusion the last output is the number of permutations displayed, not a permutation itself.

Enter a string: 1234
1234
2134
2314
2341
3241
3421
3412
4312
4132
4123
1423
1243
12

It giving 4x3=12, not the correct 4!=4x3x2x1=24. Any suggestions? Current code is:

Rich (BB code):
void permute_string(int string_size){
    char swap1;
    char swap2;
    int a=0;    //Points to 1st char being swapped
    int b=1;    //Points to 2nd char being swapped
    int n=string_size-1;    //The last element of the array

    int char_count=0;    //keeps tracks of char in the original string is being used
    int permutes=0;    //Counts how many permutations have been displayed for the string
    //int swaps=string_size;    //Keeps track of how times a char has been switched place with other chars in the string
    char givenchars[string_size];
    char acopy[string_size];   //a duplicate of the original user input

    cout<<"Enter a string: ";
    for(int i=0; i<string_size; i++){
        cin>>givenchars; //Store original string input into array.
        acopy = givenchars; //copies original string input into a backup array

    }

    while(char_count<string_size){
        for(int i=0; i<string_size; i++){
            cout<<givenchars;
        }
        cout<<endl;

        permutes++;

            swap1=givenchars[a];
            swap2=givenchars;

            //switch order
            givenchars[a]=swap2;
            givenchars=swap1;

            if(b<n){
                a++;
                b++;
            }
            else{
                a=0;
                b=1;
                char_count++;
            }

    }
    cout<<permutes;
}
 
Top