Contar las ocurrencias de una palabra en una string

Se le da una string y una palabra, su tarea es contar el número de ocurrencias de la palabra dada en la string e imprimir el número de ocurrencias de la palabra. 
Ejemplos: 

Input : string = "GeeksforGeeks A computer science portal for geeks"
word = "portal"
Output : Occurrences of Word = 1 Time

Input : string = "GeeksforGeeks A computer science portal for geeks"
word = "technical" 
Output : Occurrences of Word = 0 Time

Acercarse:

C++

// C++ program to count the number
// of occurrence of a word in
// the given string
#include <bits/stdc++.h>
using namespace std;
  
int countOccurrences(char *str, 
                    string word)
{
    char *p;
  
    // split the string by spaces in a
    vector<string> a;
  
    p = strtok(str, " ");
    while (p != NULL)
    {
        a.push_back(p);
        p = strtok(NULL, " ");
    }
  
    // search for pattern in a
    int c = 0;
    for (int i = 0; i < a.size(); i++)
  
        // if match found increase count
        if (word == a[i])
            c++;
    return c;
}
  
// Driver code
int main()
{
    char str[] = "GeeksforGeeks A computer science portal for geeks ";
    string word = "portal";
    cout << countOccurrences(str, word);
    return 0;
}
  
// This code is contributed by
// sanjeev2552

Java

// Java program to count the number 
// of occurrence of a word in
// the given string
import java.io.*;
  
class GFG {
  
static int countOccurrences(String str, String word) 
{
    // split the string by spaces in a
    String a[] = str.split(" ");
  
    // search for pattern in a
    int count = 0;
    for (int i = 0; i < a.length; i++) 
    {
    // if match found increase count
    if (word.equals(a[i]))
        count++;
    }
  
    return count;
}
  
// Driver code
public static void main(String args[]) 
{
    String str = "GeeksforGeeks A computer science portal for geeks ";
    String word = "portal";
    System.out.println(countOccurrences(str, word));
}
}
  
/*This code is contributed by Nikita Tiwari.*/

Python 3

# Python program to count the number of occurrence 
# of a word in the given string
  
def countOccurrences(str, word):
      
    # split the string by spaces in a
    a = str.split(" ")
  
    # search for pattern in a
    count = 0
    for i in range(0, len(a)):
          
        # if match found increase count 
        if (word == a[i]):
           count = count + 1
             
    return count       
  
# Driver code
str ="GeeksforGeeks A computer science portal for geeks  "
word ="portal"
print(countOccurrences(str, word))

C#

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

PHP

<?php 
// PHP program to count the number 
// of occurrence of a word in
// the given string
  
function countOccurrences($str, $word) 
{
    // split the string by spaces
    $a = explode(" ", $str);
  
    // search for pattern in string
    $count = 0;
    for ($i = 0; $i < sizeof($a); $i++) 
    {
          
    // if match found increase count
    if ($word == $a[$i])
        $count++;
    }
  
    return $count;
}
  
// Driver code
$str = "GeeksforGeeks A computer science portal for geeks ";
$word = "portal";
echo (countOccurrences($str, $word));
  
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
  
// Javascript program to count the number
// of occurrence of a word in
// the given string
      
    function countOccurrences(str,word)
    {
        // split the string by spaces in a
    let a = str.split(" ");
   
    // search for pattern in a
    let count = 0;
    for (let i = 0; i < a.length; i++)
    {
    // if match found increase count
    if (word==(a[i]))
        count++;
    }
   
    return count;
    }
    // Driver code
    let str = "GeeksforGeeks A computer
    science portal for geeks ";
    let word = "portal";
    document.write(countOccurrences(str, word));
      
      
    // This code is contributed by avanitrachhadiya2155
      
</script>

Python3

# Python program to count the number of occurrence
# of a word in the given string
def countOccurrences(str, word):
  
    wordslist = list(str.split())
    return wordslist.count(word)
  
  
# Driver code
str = "GeeksforGeeks A computer science portal for geeks  "
word = "portal"
print(countOccurrences(str, word))
  
# This code is contributed by vikkycirus

Publicación traducida automáticamente

Artículo escrito por ajay0007 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *