How do you count the occurrence of a given character in a string?



Solution:
We can solve this problem by many way’s
1.     Forloop
2.    Foreach loop
3.    Indexof method
We are doing by indexof method

indexOf() : This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.

public static int AccuranceofChar(string x, char find)
        {
            int i = x.IndexOf(find);
            return i;
        }




        static void Main(string[] args)
        {
            Program objprog = new Program();
            Console.WriteLine("Char Position : "+ Program.AccuranceofChar("ShivKumar", 'v'));
            Console.ReadLine();

        }


OutPut:
Char Position : 3

Share:

No comments:

Post a Comment

Popular

Tags

Mobile

Recent Posts