text: Count occurrences of a word in string | C # program to count the number of occurrence of a word in the given string ; split the string by spaces ; search for pattern in string ; if match found increase count ; Driver code
code: using System ; class GFG { static int countOccurrences ( string str , string word ) { string [ ] a = str . Split ( ' ▁ ' ) ; int count = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( word . Equals ( a [ i ] ) ) count ++ ; } return count ; } public static void Main ( ) { string str = " GeeksforGeeks ▁ A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks ▁ " ; string word = " portal " ; Console . Write ( countOccurrences ( str , word ) ) ; } }