views:

101

answers:

3

Hello

Merry Xmas

List<string> list = new List<string>();    
        list.Add("A");
        list.Add("B");

List<string> list1 = new List<string>();    
        list.Add("a");
        list.Add("b");


    for (int i = 0; i < list.Count; i++)
    {
        // print another list items.
        for (int j = 0; j < list1.Count; j++)
        {
            Console.WriteLine("/" + list[i] + "/" + list1[j]);
        }

    }

I want to code like this string tmpS =+ list[i]; to Join the next list item togeter.

then print tmpS

but compile error CS0023: Operator '+' cannot be applied to operand of type 'string'.

How to print all the items below.(any sort is ok)

A Aa Ab Aab Aba AB ABa ABb ABab ABba B Ba Bb Bab Bba

(The Caps number No swap. the small characters should be swaped. and always follow Caps Numbers Append small characters.)

Thank you.

+1  A: 

This smells like homework.

List<string> list = new List<string>();    
list.Add("A");
list.Add("B");

List<string> list1 = new List<string>();    
list.Add("a");
list.Add("b");

string xxx = "";
for (int i = 0; i < list.Count; i++)
{
    xxx += list[i];
    Console.WriteLine(xxx);

    // print another list items.
    for (int j = 0; j < list1.Count; j++)
    {
        Console.WriteLine("/" + list[i] + "/" + list1[j]);
    }

}
slugster
Hello Slugster, Follow you guide. i found some items still can't be print.
Nano HE
+2  A: 

It's += not =+.

But You should use a StringBuilder anyway.

helium
+1  A: 

Hello, it makes a long time I did not worked on a pure algorithmic problem!

This program should do the trick:

class Program
{
    static void Main(string[] args)
    {
        List<string> uppers = new List<string>();
        uppers.Add("A");
        uppers.Add("B");

        List<string> lowers = new List<string>();
        lowers.Add("a");
        lowers.Add("b");

        List<string> combinedUppers = GetCombinedItems(uppers);
        List<string> combinedLowers = GetCombinedItems(lowers);
        List<string> combinedUppersLowers = GetCombinedList(combinedUppers, combinedLowers);

        foreach (string combo in combinedUppersLowers)
        {
            Console.WriteLine(combo);
        }

        Console.Read();
    }

    static private List<string> GetCombinedItems(List<string> list)
    {
        List<string> combinedItems = new List<string>();

        for (int i = 0; i < list.Count; i++)
        {
            combinedItems.Add(list[i]);

            for (int j = 0; j < list.Count; j++)
            {
                if (list[i] != list[j])
                {
                    combinedItems.Add(String.Format("{0}{1}", list[i], list[j]));
                }
            }
        }

        return combinedItems;
    }

    static private List<string> GetCombinedList(List<string> list1, List<string> list2)
    {
        List<string> combinedList = new List<string>();

        for (int i = 0; i < list1.Count; i++)
        {
            combinedList.Add(list1[i]);

            for (int j = 0; j < list2.Count; j++)
            {
                combinedList.Add(String.Format("{0}{1}", list1[i], list2[j]));
            }
        }

        for (int i = 0; i < list2.Count; i++)
        {
            combinedList.Add(list2[i]);

            for (int j = 0; j < list1.Count; j++)
            {
                combinedList.Add(String.Format("{0}{1}", list2[i], list1[j]));
            }
        }

        return combinedList;
    }
}

Regards.


This program gives you this output:

A Aa Aab Ab Aba AB ABa ABab ABb ABba B Ba Bab Bb Bba BA BAa BAab BAb BAba a aA aAB aB aBA ab abA abAB abB abBA b bA bAB bB bBA ba baA baAB baB baBA

Ucodia
It's cool. THANK YOU A LOT!
Nano HE
@TheRHCP. I tested more itmes added for both List1 and List2, when i add A, B, C for List1; a,b,c for List2; the script can't Pirnt more than 4 character.(Such as ABCabc / ACadc/ ABCab...can't be print. What i want to do is combined all the conditions for List1 and List2) . Currently only support the NewString <= 4 characters.
Nano HE
The algorithm I gave you cannot handle more than 2 caracters by list. To solve this you must adapt the GetCombinedItems method. I think it can be easily adpated but the algorithm to implement is a bit more complex to design because it must handle an unknown number of caracters. In fact this method is just generating a list of all possible combination of the caracters, I think you can find this kind of algortihm on the Internet.
Ucodia