सबसे पहले एक लिस्ट बनाएं -
List list = new List();
यहाँ स्ट्रिंग "xyz" है जिसके लिए हम सबलिस्ट ढूंढेंगे। लूप करते समय हम दूसरी लिस्ट डिक्लेयर करेंगे, जो हर ट्रू इटरेशन पर सबलिस्ट जेनरेट करेगी -
for (int i = 1; i < str.Length; i++) {
list.Add(str[i - 1].ToString());
List newlist = new List();
for (int j = 0; j < list.Count; j++) {
string list2 = list[j] + str[i];
newlist.Add(list2);
}
list.AddRange(newlist);
}
निम्नलिखित पूर्ण कोड है -
उदाहरण
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
string str = "xyz";
List list = new List();
for (int i = 1; i < str.Length; i++) {
list.Add(str[i - 1].ToString());
List newlist = new List();
for (int j = 0; j < list.Count; j++) {
string list2 = list[j] + str[i];
newlist.Add(list2);
}
list.AddRange(newlist);
}
list.Add(str[str.Length - 1].ToString());
list.Sort();
Console.WriteLine(string.Join(Environment.NewLine, list));
}
}
}
आउटपुट
x xy xyz xz y yz z