Count of Repeated Charaters in a String using C#.net

Naraayanan
Posted by Naraayanan under C# category on | Points: 40 | Views : 10831
  public static int result;

Function /Method:
  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;
}


call a Function likw this:
int resy = countofrepeatedchar("123456123",'4');
MessageBox.Show(resy.ToString());
Output
1.

Comments or Responses

Posted by: T.Saravanan on: 3/13/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

Kindly add the following line in your "countofrepeatedchar" method


public static int countofrepeatedchar(string inputstring, char ch)
{
char[] Inputstring = inputstring.ToCharArray();
char Ch = ch;
result = 0;
.
.
.
.
.


Reason: You are declare "result" variable as "Global". So may be the value will be maintaining after completed the method.

Login to post response