Counting occurrences of a word in a string

Counters | Python Methods and Functions | String Variables

You are given a string and a word, your task is to count the number of occurrences of the given word in the string and output the number of occurrences of the word.
Examples :

 Input: string = "Python.Engineering A computer science portal for geeks" word = "portal" Output: Occurrences of Word = 1 Time Input: string = "Python.Engineering A computer science portal for geeks" word = "technical" Output: Occurrences of Word = 0 Time 

Suitable: -

  • First we split the string into spaces in
  • Then take the variable count = 0 and in each true condition we increment the count by 1
  • Now run a loop from 0 to the length of the string and check if our string word
  • if the condition is true, then we increase the count value by 1 and print the count value at the end.

Below is the implementation of the above approach:

C++

// C++ program for counting numbers
// occurrences of a word in
// given string given string
#include & lt; bits / stdC++. h & gt;

using namespace std;

 

int countOccurences ( char * str, 

  string word)

{

char * p;

 

// split the string into spaces in

  vector & lt; string & gt; a;

 

p = strtok (str, " " );

while (p! = NULL)

{

a.push_back (p);

p = strtok (NULL, "" );

}

 

  // search for template in

int c = 0;

for ( int i = 0; i & lt; a.size (); i ++)

 

// if a match is found, increase the counter

if (word == a [ i])

C++;

return c;

}

 
// Driver code

int main ()

{

char str [] = "Python.Engineering A computer science portal for geeks" ;

string word = "portal" ;

cout & lt; & lt; countOccurences (str, word);

return 0;

}

 
// This code is provided
// sanjeev2552

Java

// Java program for counting the number
// occurrences of a word in
// given string given string

import java.io. *;

 

class GFG {

  

static int countOccurences (String str, String word) 

{

// split the line into spaces in

String a [] = str.split ( "" );

 

// search for a template in

  int count = 0 ;

for ( int i = 0 ; i & lt; a.length; i ++) 

{

// if a match is found, increment the counter

  if (word.equals (a [i]))

count ++;

}

 

  return count;

}

 
// Driver code

public static void main (String args []) 

{

String str = "Python.Engineering A computer science portal for geeks " ;

String word = "portal" ;

System.out.println (countOccurences (str, word));

}
}

 
/ * This code is provided by Nikita Tiwari. * /

Python 3

# Python program to count the number of occurrences
# words in this line of this line

  

def countOccurences ( str , word):

 

# split the line with spaces in

a = str . split ( "" )

 

  # search for template in

count = 0

for i in range ( 0 , len (a)):

 

  # if a match is found, increment the counter

  if (word = = a [i]):

count = count + 1

 

return count 

 
# Driver code

str = "Python.Engineering A computer science portal for geeks "

word = "portal"

print (countOccurences ( str , word))

C #

// C # program for counting the number
// occurrences of a word in
// given string given string

using System;

 

class GFG 

{

static int countOccurences ( string str,

string word) 

{

// split line for spaces

string [] a = str.Split ( '' );

 

// search for a pattern in a string

  int count = 0;

for ( int i = 0; i & lt; a.Length; i ++) 

  {

  

// if a match is found, increase the counter

  if (word.Equals (a [i]))

count ++;

}

 

  return count;

}

 
// Driver code

public static void Main () 

{

string str = "Python.Engineering A computer science portal for geeks" ;

string word = "portal" ;

Console.Write (countOccurences (str, word));

}
}

 
// This code was added
// ChitraNayal

PHP

& lt ;? php 
// PHP program for counting the number
// occurrences of the word
// given string given string

 

function countOccurences ( $ str , $ word

{

// split string for spaces

$ a = explode ( "" , $ str );

 

// search for a pattern in a string

  $ count = 0;

for ( $ i = 0; $ i & lt; sizeof ( $ a ); $ i ++) 

{

 

// if match found, increment counter

if ( $ word == $ a [ $ i ])

  $ count ++;

}

 

  return $ count ;

}

 
// Driver code

$ str = "Python.Engineering A computer science portal for geeks" ;

$ word = "portal" ;

echo (countOccurences ( $ str , $ word ));

 
// This code was added
// ChitraNayal
? & gt;


Output:

 1 

Link: split Python function





Tutorials