Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这个将vewels更改为$C的C项目转换为C#?_C# - Fatal编程技术网

如何将这个将vewels更改为$C的C项目转换为C#?

如何将这个将vewels更改为$C的C项目转换为C#?,c#,C#,与其说是问题,不如说是好奇。我昨天看到这篇旧文章,开始用C#来玩它。这是2011年的原始邮寄表格() 我改变了一些密码和一个计数器来计算单词中的字母总数。我被if语句卡住了。我知道这个程序可能没有任何实际用途,但我正在努力学习C#string操作 Console.WriteLine("Enter a word."); string userWord = Console.ReadLine(); Console.WriteLine(); Console

与其说是问题,不如说是好奇。我昨天看到这篇旧文章,开始用C#来玩它。这是2011年的原始邮寄表格()

我改变了一些密码和一个计数器来计算单词中的字母总数。我被if语句卡住了。我知道这个程序可能没有任何实际用途,但我正在努力学习C#string操作

Console.WriteLine("Enter a word.");
        string userWord = Console.ReadLine();
        Console.WriteLine();
        Console.WriteLine("You wrote {0}", userWord);
        Console.WriteLine();

        userWord.ToLower();
        char[] wordArray = userWord.ToArray();


        for (int i = 0; i < wordArray.Length; i++)
        {
            string theLetter = userWord.Substring(i, 1);

            theLetter = theLetter.ToLower();

        if (wordArray[i] == 'a' || wordArray[i] == 'e' || wordArray[i] == 'i' || wordArray[i] == 'o' || wordArray[i] == 'u')
        {
                wordArray[i] = '$';

        }

            string rebuilt = new string(wordArray);

            Console.WriteLine("Your word is now: {0}", rebuilt);
            Console.WriteLine("The total number of letters in your word is {0}", userWord.Length);

        }
        Console.ReadLine();
Console.WriteLine(“输入一个单词”);
字符串userWord=Console.ReadLine();
Console.WriteLine();
WriteLine(“您编写了{0}”,userWord);
Console.WriteLine();
userWord.ToLower();
char[]wordArray=userWord.ToArray();
for(int i=0;i

我只想把元音改成$或任何其他字母或符号,然后计算单词中的数字

链接到的帖子中的C程序的第一个问题是,它只将小写元音更改为“$”,而不是大写元音。第二个问题是C#中的字符串是不可变的,您已经解决了这个问题,将单词更改为数组,修改该数组,然后从修改后的数组中创建新字符串

将所有元音转换为“$”的方法可能如下所示:

public static string VowelsToSymbol(string input)
{
    if (string.IsNullOrWhiteSpace(input)) return input;
    var work = new char[input.Length];
    for (int i = 0; i < work.Length; i++)
    {
        var c = input[i];
        switch (c)
        {
            case 'A': case 'E': case 'I': case 'O': case 'U':
            case 'a': case 'e': case 'i': case 'o': case 'u':
                work[i] = '$'; break;
            default:
                work[i] = c; break;
        }
    }
    return new string(work);
}

如果你想允许使用重音字符(或者在英语以外的其他语言中可能算作元音的任何字符),事情很快就会变得难看,你最好使用另一种方法(在互联网上搜索“.NET”is元音以查找示例)。

其核心是替换操作。唯一的大问题是元音是什么(以及这种语言是否有元音)。因此,我认为String.Replace可以做到这一点,LINQ或自定义编写的循环也可以做到这一点。只需避免foreach循环,因为如果集合被更改,它将不起作用。myString.Replace(oldChar,newChar)?在深入研究更复杂的主题(如字符串操作)之前,请阅读,以便您能够理解所使用语言的基础知识(在本例中为C#)。感谢您的链接。我会在晚些时候再发这个。我想我可能有点太野心勃勃了。
private static char OneVowelToSymbol(char c)
{
    switch (c)
    {
        case 'A': case 'E': case 'I': case 'O': case 'U':
        case 'a': case 'e': case 'i': case 'o': case 'u':
            return '$';
        default:
            return c;
    }
}

public static string VowelsToSymbolLinq(string input)
{
    return string.IsNullOrWhiteSpace(input) ? input :
        new string(input.Select(OneVowelToSymbol).ToArray());
}