如何计算字符串中的字母出现? [英] How to count letter occurances in string?

查看:68
本文介绍了如何计算字符串中的字母出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经有了一个代码,用于计算字符串中字母的出现次数。现在我不知道如何制作这样一个符号(就像有多少!)。我有一个字母和一个自定义字母Ą的字符串,所以我需要知道如何使用R(字符串R =...)中的字母来计算控制台中字母的出现次数。我听说最好的方法是使用indexof(),但我不知道如何使用它?

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Collections;
使用System.IO;

namespace _5._1
{
class LetterOccur
{
private const int CMax = 256;
private int [] Rn;
public string eil {get; set;}
public string R =AĄBCDEFGHIJKLMNOPRSTUVQZW;
public LetterOccur()
{
eil =;
Rn = new int [CMax];
for(int i = 0; i< CMax; i ++)
{
Rn [i] = 0;
}
}
public int Take(char s im)
{
返回Rn [sim];
}

public void many()
{
for(int i = 0; i< eil.Length; i ++)
{
if(('A'< = eil [i]&& eil [i]< ='Z')||(('a'< = eil [i]&& eil [ i]< ='z')))
{
Rn [eil [i ++]] ++;
}
}
}
}
类程序
{
const string CFr =.. \\\\ \Rezults.txt;
static void Main(string [] args)
{
LetterOccur eil = new LetterOccur();
string R =AĄBCDEFGHIJKLMNOPRSTUVQZW;
Console.WriteLine(输入一行单词:);
string line = Console.ReadLine();
eil.eil = line;
eil.many();
print(CFr,eil,R);
}
static void print(string fv,LetterOccur eil,string R)
{
using(var fr = File.CreateText(fv))
{
for(char sim ='a'; sim< ='z'; sim ++)
{
fr.WriteLine({0,3:c} {1,4:d} | {2,3:c} {3,4:d},
sim,eil.Take(sim),
Char.ToUpper(sim),eil.Take(Char.ToUpper(sim) ));
}
}
}
}
}





我尝试了什么:



不知道如何计算C#中的自定义字符

解决方案

< blockquote>

如何让你的代码更简单,例如:



string your_string =abc!defgh!jkl ;

  //  解决方案1 ​​ - 删除角色并减去长度。 
int result1 = your_string.Length - your_string.Replace( )。长度;

// 解决方案2 - 使用目标字符将字符串拆分为数组分隔符
int result2 = your_string.Split(' !')。长度 - 1 ;

// 解决方案3 - 使用LINQ
int result3 = your_string.ToCharArray()。Count(c = > c == ' !');





干杯,

AH


看看这个:计算字符串中的行数 [ ^ ]它基于行(所以有些位你可以忽略)但是换行只是文本文件中的一个字符,因此大多数显示的方法都适用于任何角色。



要知道你的字符串中每个字符的数量在一行中,这将有效:

 string input =ABCDABDFGHI; 
Dictionary< char,int> counts = input.GroupBy(x => x).ToDictionary(x => x.Key,x => x.Count());

字典的每个元素都是字符和出现的次数。


So i already have a code which counts how many occurances there are of a letter in string. Now i don't know how to make so that it counts a symbol(like how many "!" there are). I have a string with alphabet and one custom letter "Ą", so i need to know how do i use those letters from "R"("string R = "..." ) to count occurances of a letter in the console. I heard the best way is to use indexof() but i have no clue on how to use it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace _5._1
{
    class LetterOccur
    {
        private const int CMax = 256;
        private int[] Rn;
        public string eil { get; set; }
        public string R = "AĄBCDEFGHIJKLMNOPRSTUVQZW";
        public LetterOccur()
        {
            eil = "";
            Rn = new int[CMax];
            for (int i = 0; i < CMax; i++)
            {
                Rn[i] = 0;
            }
        }
        public int Take(char sim)
        {
            return Rn[sim];
        }

        public void many()
        {
            for (int i = 0; i < eil.Length; i++)
            {
                if (('A' <= eil[i] && eil[i] <= 'Z')||(('a' <= eil[i] && eil[i] <= 'z')))
                {
                    Rn[eil[i++]]++;
                }
            }
        }
    }
    class Program
    {
        const string CFr = "..\\..\\Rezults.txt";
        static void Main(string[] args)
        {
            LetterOccur eil = new LetterOccur();
            string R = "AĄBCDEFGHIJKLMNOPRSTUVQZW";
        Console.WriteLine("Enter a line of words: ");
            string line = Console.ReadLine();
            eil.eil = line;
            eil.many();
            print(CFr, eil, R);
        }
        static void print(string fv, LetterOccur eil, string R)
        {
            using (var fr = File.CreateText(fv))
            {
                for (char sim = 'a'; sim <= 'z'; sim++)
                {
                    fr.WriteLine("{0, 3:c} {1, 4:d} |{2, 3:c} {3, 4:d}",
                    sim, eil.Take(sim),
                    Char.ToUpper(sim), eil.Take(Char.ToUpper(sim)));
                }
            }
        }
    }
}



What I have tried:

Dont know how to count custom characters in C#

解决方案

Hi,
How about trying to make your code more simple, for example:

string your_string = "abc!defgh!jkl";

//Solution 1 - remove the character and subtract the length.
int result1 = your_string.Length - your_string.Replace("!", "").Length;

//Solution 2 - split the string into an array by using your target character as a delimiter
int result2 = your_string.Split('!').Length - 1;

//Solution 3 - use the LINQ 
int result3 = your_string.ToCharArray().Count(c => c == '!');



Cheers,
AH


Have a look at this: Counting Lines in a String[^] it's based on lines (so there are some bits you can ignore) but newline is just a character in a text file, so most oif the methods it shows will work for any character.

To find out how many of each character there are in your string in one line, this will work:

string input = "ABCDABDFGHI";
Dictionary<char, int> counts = input.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

Each element of the dictionary will be the character and how many times it appears.


这篇关于如何计算字符串中的字母出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆