从输入中计数字符时,在输出中获取空格字符

本文关键字:字符 输出 获取 空格 数字 输入 | 更新日期: 2023-09-27 17:58:11

我已经处理了几天了,我正在尝试完成一个控制台应用程序,在该应用程序中,我们会被提示键入自己的字符串,输出会列出每个唯一字符,并在其后面加上一个计数。在结果结束时,会显示一个计数,显示找到的唯一字符数。所有内容都被翻译成小写,不管它是否为大写。他们的关键是使用集合。这是我迄今为止所拥有的。我的输出在结果中显示了两个空格字符,尽管我使用了if语句来捕捉它们。有人能指出我忽略的一个概念吗?

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class LetterCountTest
{
    public static void Main(string[] args)
    {
        // create sorted dictionary based on user input
        SortedDictionary<string, int> dictionary = CollectLetters();
        // display sorted dictionary content
        DisplayDictionary(dictionary);
    } // end Main
    // create sorted dictionary from user input
    private static SortedDictionary<string, int> CollectLetters()
    {
        // create a new sorted dictionary
        SortedDictionary<string, int> dictionary =
           new SortedDictionary<string, int>();
        Console.WriteLine("Enter a string: "); // prompt for user input
        string input = Console.ReadLine(); // get input
        // split every individual letter for the count
        string[] letters = Regex.Split(input, "");
        // processing input letters
        foreach (var letter in letters)
        {
            string letterKey = letter.ToLower(); // get letter in lowercase
            if (letterKey != " ") // statement to exclude whitespace count
            {
                // if the dictionary contains the letter
                if (dictionary.ContainsKey(letterKey))
                {
                    ++dictionary[letterKey];
                } // end if
                else
                    // add new letter with a count of 1 to the dictionary
                    dictionary.Add(letterKey, 1);
            }
        } // end foreach
        return dictionary;
    } // end method CollectLetters
    // display dictionary content
    private static void DisplayDictionary<K, V>(
       SortedDictionary<K, V> dictionary)
    {
        Console.WriteLine("'nSorted dictionary contains:'n{0,-12}{1,-12}",
           "Key:", "Value:");
        // generate output for each key in the sorted dictionary
        // by iterating through the Keys property with a foreach statement
        foreach (K key in dictionary.Keys)
            Console.WriteLine("{0,-12}{1,-12}", key, dictionary[key]);
        Console.WriteLine("'nsize: {0}", dictionary.Count);
        Console.ReadLine();
    } // end method DisplayDictionary
} // end class LetterCountTest

我的输出表明,我使用了字母表中的每个字母,但在"a"和它的两个实例上方也有一个空白。我不知道这是从哪里来的,但我猜它是在计算null字符或回车。我使用的字符串是…

敏捷的棕色狐狸跳过懒狗

除了每一个字母计算一次外,它还计算e三次,h两次,o四次,r两次,t两次,u两次。

从输入中计数字符时,在输出中获取空格字符

您的问题依赖于Regex.Split()的行为。正在从方法的msdn页面中摘录。。。

如果在输入字符串的开头或结尾找到匹配项,则在返回数组的开头或末尾会包含一个空字符串。

这正是当您调用Regex.Split(input, "");时发生的情况。为了解决这个问题,您可以从代码中删除正则表达式匹配,将字典的所有实例更改为SortedDictionary<char, int>,并使用foreach循环迭代输入字符串的每个字符。

foreach (char letter in input.ToLower())
{
    if (!Char.IsWhiteSpace(letter))
    {
         //Do your stuff
    }
}

由于string已经是一个数组或字符,您不需要该Regex.Split,只需使用:

foreach (var letter in input)

然后将if语句更改为:

 foreach (var letter in input)
 {
     if (!char.IsWhiteSpace(letter))
     {
         var letterKey = letter.ToString();
         if (dictionary.ContainsKey(letterKey))
         {
             ++dictionary[letterKey];
         } 
         else
             dictionary.Add(letterKey, 1);
    }
} 

然后它应该排除空字符串和空白。我怀疑Regex.Split之后会有一些空字符串,并且只检查空白,因此会得到意外的结果。如果您使用chars,则不需要担心空字符串。