how to counts the character in string [Resolved]

Posted by Gow.Net under C# on 3/9/2012 | Points: 10 | Views : 20229 | Status : [Member] | Replies : 10
how to write program that counts the number of occurrences of a particular character in a line of text

gowthaman8870226416


Responses

Posted by: Naraayanan on: 3/13/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,
Check this code
   public static int result;

public static int countofrepeatedchar(string inputstring, char ch)

{
char[] Inputstring = inputstring.ToCharArray();
char Ch = ch;
int cntofchar = Inputstring.Length;
foreach (char chr in Inputstring)
{
if (chr == Ch)
{
result++;
}

}


return result;
}



   int resy = countofrepeatedchar("Narayanan",'a');

MessageBox.Show(resy.ToString());



Output:
4.

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

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

Posted by: CGN007 on: 3/13/2012 [Member] Silver | Points: 50

Up
0
Down

Resolved
Use a regular expression to count the occurrence

 string inputString= "Testinggg";

string CharacterToFind = "g";
Regex exp = new Regex(CharacterToFind, RegexOptions.IgnoreCase);
int noOfOccurrences = exp.Matches(inputString).Count;


Please add the namespace 'using System.Text.RegularExpressions' on top...!!!

Hope this works for you...

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

Posted by: Sksamantaray on: 3/13/2012 [Member] Silver | Points: 50

Up
0
Down

Resolved

Hi ,
Try this.
 protected void Page_Load(object sender, EventArgs e)

{
int count = 0;
string st = "sanjay";
foreach (char a in st)
{

if (char.Equals(a,'a'))
{
count++;
}
}
Response.Write(count.ToString());
}


Thanks,
Sanjay

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

Posted by: Raj_Chennai on: 3/10/2012 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string s = "arunkumar";
char c = 'a';
int co = 0;
for (int i= 0; i < s.Length - 1; i++)
{
if (s[i] == c)
{
co++;
}
}
Console .Write ("The Character " + c.ToString() + " is repeated for " + co + " times");
}
}
}


raj

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

Posted by: Gow.Net on: 3/10/2012 [Member] Starter | Points: 25

Up
0
Down
hi raj
i get this error
Operator '==' cannot be applied to operands of type 'string' and 'char'
and it not possible for
if (s == c) 

{
co++;
}

this condition because string and char not same



gowthaman8870226416

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

Posted by: Shubham on: 3/10/2012 [Member] Starter | Points: 25

Up
0
Down
plz try the boolen expression bro because" == "not applied on string or char data type thank you



if (s==1)
{
co++;
}
this is worked proper


Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

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

Posted by: Gow.Net on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
thank you sir

gowthaman8870226416

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

Posted by: Gow.Net on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
hi Shubham sir

in your code how possible string value equal to integer

if (s==1)  //not possible

{
co++;
}


gowthaman8870226416

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

Posted by: Mrinal on: 3/23/2012 [Member] Starter | Points: 25

Up
0
Down
class SearchDemo
{

public static void Main(string[] args)
{
string str = "This is my name";
Char chr = 's';
Console.WriteLine(SearchDemo.CharOccurs(str, chr));
Console.Read();

}

public static int CharOccurs(string stringToSearch, char charToFind)
{
int count = 0;
char[] chars = stringToSearch.ToCharArray();
foreach (char c in chars)
{
if (c == charToFind)
{
count++;
}
}
return count;
}
}

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

Posted by: Firemyst on: 4/13/2014 [Member] Starter | Points: 25

Up
0
Down
Check out this http://blogs.davelozinski.com/curiousconsultant/csharp-fastest-way-count-number-times-character-occurs-string too if you're curious for the fastest way to count the number of times a character occurs in a string.

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

Login to post response