0
using System;

class HelloWorld
{
    static void Main(String[] args)
    {
        countSearchCharInInString("Stanley",'s'); //return 1
    }

    //inString = "Stanley", searchChar = 't', return 1
    static int countSearchCharInInString(string inString, char searchChar)
    {
        foreach (char letter in countSearchCharInInString 
        {
            for (int i = 0; i = inString.lengeth; i++) 
            {
                string count = inString{i}
                if (count = searchChar)
                string newString = [];
                newString.Add(count);
                console.writeline(newString.length)
            }
        };
        return 0;
    }
}

I am trying to count how many times the letter s is present in this string name Stanley. I havent used c# in a while and was wondering if there was ashorter way of doing this. Also, this doesn't work I know but it was just me brainstorming the concept. Any help would be greatly appreciated.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Perry Craft
  • 309
  • 3
  • 15
  • 1
    I'm not sure that duplicate is correct. OP needs to find how many characters in the entire string, most of the answers in that question are under the impression that the string with _start with_ a certain character and needs that count. The context is different. This is not to say that there is not a duplicate question out there, I would bet on there being one. I just think a more contextual duplicate could be found. – maccettura Oct 10 '17 at 20:06
  • 1
    `var count = "Stanley".Count(c => c == 't');` – L.B Oct 10 '17 at 20:07
  • 1
    @maccettura Did you refresh your browser? I have already added this question that you referenced and edited the duplicate links. – Salah Akbari Oct 10 '17 at 20:10

0 Answers0