Categories

Tools

bitdefender

1&1 Web Hosting

Count the number of occurrences of the characters in a text, with ASP.NET C #


The example allows you to count the number of occurrences of the characters in a text.

Code:

/// <summary>
/// Count the number of occurrences of the characters in a text.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonCount_Click(object sender, EventArgs e)
{
	if (TextBoxSearch.Text != "")
	{
		string result = String.Empty;
		string phrase = TextBoxSearch.Text;
		phrase = phrase.Replace(" ","");
		phrase = phrase.ToUpper();
		char[] c = phrase.ToCharArray();
		Array.Sort(c);

		int index = 0;
		char[] co = new char[c.Length];
		int[] oc = new int[c.Length];

		for (int n = 0; n <c.Length; n++)
		{
			char currentChar = c[n];
			
			if ( IsInArray(co, currentChar) )
			{
				// Update value occurrence of character
				UpdateOccurrence(co, oc, currentChar);
			}
			else
			{
				// Add a new value in the array
				co[index] = currentChar;
				oc[index] = 1;
				index++;
			}
		}

		for (int n = 0; n < co.Length; n++)
		{
			if (oc[n]!=0)
			result += co[n] + " = "+oc[n] + "<br />";
		}

		LabelMessage.Text = result;
		TextBoxSearch.Focus();
	}
}

/// <summary>
/// Update value occurrence of character
/// </summary>
/// <param name="co"></param>
/// <param name="oc"></param>
/// <param name="currentChar"></param>
private void UpdateOccurrence(char[] co, int[] oc, char currentChar)
{
	for (int n = 0; n < co.Length; n++)
	{
		if (currentChar == co[n])
		{
			oc[n] = oc[n] + 1;
		}
	}
}

/// <summary>
/// Check whether the character is in the matrix
/// </summary>
/// <param name="co"></param>
/// <param name="currentChar"></param>
/// <returns></returns>
private bool IsInArray(char[] co, char currentChar)
{
	bool isInArray = false;
	for (int n = 0; n < co.Length; n++)
	{
		if (currentChar == co[n])
			isInArray = true;
	}
	return isInArray;
}

Result of this example:

Count the number of occurrences of the characters in a text, with ASP.NET C #

Posted in ASP.NET by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.