我正在制定一种解决方案,其中用户输入一个单词或仅输入字母,并且它显示单词的排列而不重复。我怎么做才能使输入变成一个字符数组(我正在修改其他人的代码以尝试执行此操作),所以我想给一个给定值,在下面的代码中我在SOMEHOW处添加了注释,以便我可以拆分将其放入数组。我是编码新手,不胜感激。
public static void Main()
{
string input;
//char word_inputed;
Console.WriteLine("Please print a word");
input = Console.ReadLine();
//string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = input.Split(' ');
foreach (var word in words)
{
foreach (var letter in word)
{
//SOMEHOW values = Console.Write($"{letter},");
}
}
ArrayList list = new ArrayList();
list.AddRange(values.Split(new char[] { ',' }));
char[] word_inputed = { 'A' };
int n = word_inputed.Length;
findPermutations(word_inputed, 0, n);
}
这是Microsoft示例
// Sample for String.ToCharArray(Int32, Int32)
using System;
class Sample {
public static void Main() {
string str = "012wxyz789";
char[] arr;
arr = str.ToCharArray(3, 4);
Console.Write("The letters in '{0}' are: '", str);
Console.Write(arr);
Console.WriteLine("'");
Console.WriteLine("Each letter in '{0}' is:", str);
foreach (char c in arr)
Console.WriteLine(c);
}
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/