Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,
C#
string pattren = " INC | TRUST | COMPANY | 401K ";
string[] ArrPattern = pattren.Split('|');

Above the string array i need to concatenate all the posibilities like below output.

ex:
INC PLAN COMPANY 401K
INC PLAN COMPANY empty
INC PLAN empty 401k
INC PLAN empty empty
INC empty COMPANY 401k
INC empty empty 401k
INC empty empty empty
empty PLAN COMPANY 401k
...
...
...

Like these 16 combination.

Please share your ideas its very urgent for me.

Regards,
Ram
Posted
Updated 13-May-13 3:35am
v2
Comments
Simon_Whale 13-May-13 9:53am    
please dont cross post it is considered rude

http://www.codeproject.com/Messages/4563297/string-concatenation-with-all-possibilities.aspx

I am not sure, but take a look at this thread about permutations.
 
Share this answer
 
At first glance, it looks simple to me.

1) Compute number of possible combinations
C#
int combinations = 2 << ArrPattern.Length;

2) Use a loop to combinations to get all combinations
3) Within the loop, let the individual binary digits decide whether you use an actual word or an empty string.
C#
for ( int i = 0; i < combinations; i++)
{
    StringBuilder singleVariant = new StringBuilder();
    for( int stringIndex = 0; stringIndex < ArrPattern.Length; stringIndex++)
    {
        if( (stringIndex & i) != 0)
        {
            singleVariant.Append(ArrPattern[stringIndex]);
        }
        else
        {
            singleVariant.Append(string.empty);
        }
    }
}

4) Add error handling, space stuffing, bug fixing, input, output, etc
 
Share this answer
 
Comments
sethupathiram 14-May-13 20:58pm    
thanks lot lukeer for kind response.Please clarify below doubts,

While i was applying your code it is not considering pattern array first character...

below output i am getting,

Empty Trust company 401k
Empty empty company 401k
Empty trust empty 401k
......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900