Change language

Counting occurrences of a word in a string

| |

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 "bits / stdC++. h"

using namespace std;

 

int countOccurences ( char * str, 

  string word)

{

char * p;

 

// split the string into spaces in

  vector "string" 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 "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 ""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 "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 "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 "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
?"


Output:

 1 

Link: split Python function

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically