📌  相关文章
📜  计算字符串中某个单词的出现次数

📅  最后修改于: 2022-05-13 01:55:37.547000             🧑  作者: Mango

计算字符串中某个单词的出现次数

给你一个字符串和一个单词,你的任务是计算字符串中给定单词的出现次数并打印该单词的出现次数。
例子:

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

方法:

  • 首先,我们用空格分割字符串
  • 然后,取一个变量 count = 0 并且在每个真实条件下我们将计数增加 1
  • 现在在 0 到字符串长度处运行一个循环并检查我们的字符串是否等于单词
  • 如果条件为真,那么我们将 count 的值加 1,最后,我们打印 count 的值。

以下是上述方法的实现:

C++
// C++ program to count the number
// of occurrence of a word in
// the given string
#include 
using namespace std;
 
int countOccurrences(char *str,
                    string word)
{
    char *p;
 
    // split the string by spaces in a
    vector 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


Javascript


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


输出:

1

方法 #2:在Python中使用Count()函数:

  • 首先,我们用空格分割字符串并存储在列表中。
  • 我们使用 count() 来查找列表中该单词的计数。

下面是实现:

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
输出
1

参考:分割函数Python