2 Attachment(s)
Count character Occurrences in a string
I have seen this question many times so thought it would be good to post the following code. There are two versions of the code as per below.
Code:
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CountCharacters
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// With encoding
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//string items = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(textBox1.Text);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string itemsTranslated = iso.GetString(isoBytes);
var characterGroup = (
from chr in itemsTranslated.ToCharArray()
group chr by chr into grp
select new
{
Letter = grp.Key,
Occurrences = grp.Count(),
Code = Convert.ToInt32((int)grp.Key)
})
.ToList()
.OrderBy((item) => item.Letter.ToString());
var results = (from item in characterGroup select item).ToList();
dataGridView1.DataSource = results;
}
/// <summary>
/// Without encoding
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
var characterGroup = (
from chr in textBox1.Text.ToCharArray()
group chr by chr into grp
select new
{
Letter = grp.Key,
Occurrences = grp.Count(),
Code = Convert.ToInt32((int)grp.Key)
})
.ToList()
.OrderBy((item) => item.Letter.ToString());
dataGridView1.DataSource = (from item in characterGroup select item).ToList();
}
}
}
Full source in VS2013
Attachment 131535
Re: Count character Occurrences in a string
Hello, @karenInstructor
Please try this code, To Count character Occurrences in a string
Code:
using System;
public class GFG {
// Method that return count of the given
// character in the string
public static int count(string s, char c)
{
int res = 0;
for (int i = 0; i < s.Length; i++)
{
// checking character in string
if (s[i] == c)
res++;
}
return res;
}
// Driver method
public static void Main()
{
string str ="GoodMorning!";
char c = 'o';
Console.WriteLine(count(str, c));
}
}
I hope this code will be useful to you.
Thank you.
Re: Count character Occurrences in a string
Quote:
Originally Posted by
Sherin
I hope this code will be useful to you.
You can post whatever you want, wherever you want, as long as it is within site guidelines. That said:
1. This thread is over five years old. Posting to such old threads may help others who find it in future but it's not likely to help the OP.
2. This thread is in the CodeBank, so it is sharing working code rather than asking for help with a problem.
3. Your code doesn't do what the code in post #1 does. Your code will count the occurrences of a specific character only. The original code provides a count of each character in the text. To do that with your code, you'd first have to know which characters were in the text, or else you'd have to search for every single possible character and then discard the zero counts. You'd also have to search the entire text once for each character, making it much slower.
4. Your code isn't nearly the most succinct way to do what it actually does, which would be:
vb.net Code:
Console.WriteLine(str.Count(ch => ch == c));
No need for the dedicated method at all, as the existing Enumerable.Count method can already do effectively the same thing.