Adan
Adan

Reputation: 473

c# How to print the chars of strings in arraylist

This program prints all possibilities of a string input, for example, if input is abc', the output should be the combinations, this works but when im creating the arraylist it's printing out numbers instead of the string combinations, code:

string input = Console.ReadLine();

int sl = input.Length;
ArrayList arr = new ArrayList();

for (int three = 0; three < sl; three++) {
    for (int two = 0; two < sl; two++) {
        for (int one = 0; one < sl; one++) {
            char onef = input[one];
            char twof = input[two];
            char threef = input[three];


            arr.Add(threef + twof + onef);

            Console.Write(threef);
            Console.Write(twof);
            Console.WriteLine(onef);
        }
    }
}

Console.WriteLine("The elements of the ArrayList are:");
foreach(object obj in arr) {
    Console.WriteLine(obj);
}

the output of the arraylist is numbers and not the string chars, help!

Upvotes: 1

Views: 532

Answers (2)

Marc L.
Marc L.

Reputation: 3399

Alternate: change one line:

arr.Add(string.Format("{0}{1}{2}", a, b, c));

char cannot be concatenated like string. The + is interpreted numerically.

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222682

Just change these three lines

From:

 char onef = input[one];
 char twof = input[two];
 char threef = input[three];

To:

  string onef = input[one].ToString();
  string twof = input[two].ToString();
  string threef = input[three].ToString();

Upvotes: 2

Related Questions