Count the individual characters in a string

Posted by Mani654mani under C# on 5/31/2012 | Points: 10 | Views : 1299 | Status : [Member] | Replies : 6
Hi,


I have a string as given below:

string str = "sssmmffgggggg"

but i need to display the output as given below by using c#.


Ouput is:

s = 3
m = 2
f = 2
g = 6

Thanks in advance.




Responses

Posted by: Sakthi.Singaravel on: 5/31/2012 [Member] Silver | Points: 25

Up
0
Down
Imports System.IO

Module Module1
Sub Main()
Dim tempBig As Integer = 0
Dim tempChar As Char = vbNullChar
Dim str As String = "Hello"
Dim tempCount As Integer = 0
For Each ch As Char In str
tempCount = 0
For Each ch1 As Char In str
If ch = ch1 Then
tempCount += 1
End If
Next
Console.WriteLine("Char : " + ch)
Console.WriteLine("Count : " + tempCount.ToString)
Next
Console.Read()
End Sub
End Module


Output:

Char : H
Count : 1
Char : e
Count : 1
Char : l
Count : 2
Char : o
Count : 1

Regards,
Singaravel M

Mani654mani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 5/31/2012 [Member] Bronze | Points: 25

Up
0
Down
http://www.dotnetfunda.com/codes/code2605-count-the-particular-word.aspx

Search it you may geet some idea

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Mani654mani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 5/31/2012 [Member] Bronze | Points: 25

Up
0
Down
http://www.dotnetfunda.com/codes/code2604-code-for-count-the-string.aspx

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Mani654mani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 5/31/2012 [Member] Bronze | Points: 25

Up
0
Down
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
public static class Program
{

static void Main(string[] args)
{
string aa = "sabarimahesh";
char aaa = 'a';
Console.WriteLine(Individualchar(aa, aaa));
Console.ReadLine();
}

public static int Individualchar(string srtingsr, char Charfind)
{
int cnt = 0;
char[] ch = srtingsr.ToCharArray();
foreach (char c in ch)
{
if (c == Charfind)
{
cnt++;
}
}
return cnt;
}


}
}


Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Mani654mani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 5/31/2012 [Member] Silver | Points: 25

Up
0
Down
r u got solution...?

Regards,
Singaravel M

Mani654mani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 5/31/2012 [Member] Silver | Points: 25

Up
0
Down
are u got answer using my suggesstion...

Regards,
Singaravel M

Mani654mani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response