开发者

all possible combinations in a string in C

开发者 https://www.devze.com 2023-04-05 02:11 出处:网络
I am trying to print all possible combinations of the string \'abc\' using C. Can someone help point out where in this code I am going wrong ? I am using the algorithm mentioned here:

I am trying to print all possible combinations of the string 'abc' using C. Can someone help point out where in this code I am going wrong ? I am using the algorithm mentioned here: http://hackercs.com/videos/Combinations-of-a-String-Part-2/ Thanks for your time and help. ( BTW, the goal is to use recursion here)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


 void combination(char **curString,int allowedStart,char **outBuffer,int length)
{
//check for allowed chars.
// if not return.
// else
// for each char in the allowed char
// copy into buffer
//print buffer
//combine buffer and over next allowed chars
//remove from buffer.

if (allowedStart == length)
    return;
else    
   {
int curr;
for (curr = allowedStart;curr<length; curr++){
//need to copy 'a' into outbuffer and keep appending to outbuffer.
printf("allowedStart = %d\n",allowedStart);
printf开发者_JAVA技巧("curr = %d\n",curr);

(*outBuffer)[curr] = (*curString)[allowedStart];
printf("outbuff is %s\n",*outBuffer);       

combination(curString,curr+1,outBuffer,length);
    printf("return\n");
(*outBuffer)[length-1] = '\0';

   } //else
} //for 
}

 main()
 {
 char *var = "abc";
 int length = strlen(var);
 printf("length = %d\n",length);

 char *outBuffer = malloc ( length * sizeof (char));
 bzero(outBuffer,3);
 combination(&var,0,&outBuffer,length);

  }


For starters, you're going wrong here:

 char *var = "abc";
 int length = strlen(var);
 printf("length = %d\n",length);

 char *outBuffer = malloc ( length * sizeof (char));
 bzero(outBuffer,3);

This is very confused code. It's mixing dynamic buffer length handling (the strlen() call) with static ones (the 3 in the bzero() call). It's also Doing It Wrong, by using sizeof (char) (which is guaranteed to be 1 by the C language, and thus just adds noise and confusion). Also, the number of characters needed to hold a 3-character printable string in C is not 3, but 4 since you need one character for the terminating '\0'.


Got this working.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void combination(char **curString,int allowedStart,char **outBuffer,int length)
{
    //check for allowed chars.
    // if not return.
    // else
    // for each char in the allowed char
    // copy into buffer
    //print buffer
    //combine buffer and over next allowed chars
    //remove from buffer.
    int curr;

    if ( allowedStart == length )
        return;
    else    {

            for (curr = allowedStart;curr<length; curr++){
        //need to copy 'a' into outbuffer and keep appending to outbuffer.
        //      printf("allowedStart= %d  curr= %d \n",allowedStart,curr);

                (*outBuffer)[curr] = (*curString)[allowedStart];
                //adjust for appending.

                int i = 0;
                while (i < length)
                {
                    if ( ((*outBuffer)[i] == '\0') && ((*outBuffer)[i+1] != '\0') )
                    {
        //              printf("in here\n");
                        (*outBuffer)[i] = (*outBuffer)[i+1];
                        (*outBuffer)[i+1] = '\0';
                    }
                    i++;
                }

        //      printf("added curr%d %c \n",curr, (*outBuffer)[curr]);
                printf("***************************************COMBO: %s\n",*outBuffer);        
                allowedStart = curr+1;
        //      printf("allowedStart%d %c \n",allowedStart,(*curString)[allowedStart]);

                combination(curString,allowedStart,outBuffer,length);
        //      printf("removing%d %c\n",curr,(*outBuffer)[curr]);
                (*outBuffer)[curr] = ' ';   
        //      printf("**************RETURNCOMBO: %s\n",*outBuffer);       

        } //else
    } //for 
}

main()
{
    char *var = "abcd";
    int length = strlen(var);
    printf("length = %d\n",length);
//  printf("Intial word is %s\n",var);
    char *outBuffer = malloc ( (length+1) * sizeof (char));
    bzero(outBuffer,length);
    combination(&var,0,&outBuffer,length);

}


#include <iostream>

#define PRINTLN(STR) std::cout << STR << std::endl
#define SWAP(ARRAY, I, J) if(I != J) { ARRAY[I] ^= ARRAY[J]; ARRAY[J] ^= ARRAY[I]; ARRAY[I] ^= ARRAY[J]; }

void PrintCombinations_Rec(char* str, size_t idx)
{
    const size_t len = strlen(str);
    if( len == idx)
        PRINTLN(str);
    else
    {
        for (size_t i = idx; i < len; ++i)
        {
            SWAP(str, idx, i);
            PrintCombinations_Rec(str, idx + 1);
            SWAP(str, i, idx);
        }
    }
}

void PrintCombinations(const char* input)
{
    const size_t len = strlen(input);
    char* str = new char[len + 1];
    strncpy_s(str, len + 1, input, len);

    // Recursive call
    PrintCombinations_Rec(str, 0);

    delete[] str;
}

int main(int argc, char** argv)
{
    PrintCombinations("ABCD");
    return EXIT_SUCCESS;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号