What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10239 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Count of number for individual char in string ??? ...
Sabarimahesh

Count of number for individual char in string ???

 Code Snippet posted by: Sabarimahesh | Posted on: 5/31/2012 | Category: C# Codes | Views: 572 | Status: [Member] | Points: 40 | Alert Moderator   


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
Found interesting? Add this to:


 Responses

Niladri.Biswas
Posted by: Niladri.Biswas | Posted on: 6/1/2012 | Level: Diamond | Status: [Member] | Points: 10 | Alert Moderator 

Hi,
This can also be done using group by of linq

string input = "sabarimahesh";

char searchFor = 'm';

(from letter in input.ToCharArray()
group letter by letter into g
select new
{
Letters = g.Key

,
LetterCount = g.Count()
})
.Where(i => i.Letters == searchFor)
.ToList()
.ForEach(i => Console.WriteLine(i.LetterCount));


OR Lambda

input

.ToCharArray()
.GroupBy(i => i)
.Select(i => new { Letters = i.Key, LetterCount = i.Count() })
.Where(i => i.Letters == searchFor)
.ToList()
.ForEach(i => Console.WriteLine(i.LetterCount));


Best Regards,
Niladri Biswas

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 2:12:52 AM