📌  相关文章
📜  计算字符串中单词的出现次数 |设置 2(使用正则表达式)

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

计算字符串中单词的出现次数 |设置 2(使用正则表达式)

给定一个字符串str和一个单词w,任务是使用正则表达式打印字符串str中给定单词的出现次数。

例子:

方法:在给定字符串中查找所需字符串w 的计数所需的正则表达式是“\\b w \\b” ,其中\b是单词边界。按照步骤解决问题

  • 为单词w创建正则表达式模式
  • 遍历字符串,使用 regex_iterator() 将正则表达式与字符串str匹配。同时,更新匹配数。
  • 打印在上述步骤中获得的匹配总数。

以下是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to count total occurrences
// of word "w" in string "str"
void countOccurrences(string str, string w)
{
    // Get the regex to be checked
    string regexPattern = "\\b" + w + "\\b";
    const regex pattern(regexPattern);
 
    // Variable to count total
    // occurrences of the given word
    int count = 0;
 
    auto it
        = sregex_iterator(str.begin(), str.end(), pattern);
 
    for (it; it != sregex_iterator(); it++) {
 
        // Increment count
        count++;
    }
 
    // Print the occurrences of the word
    cout << count << endl;
 
    return;
}
 
// Driver Code
int main()
{
    // Input
    string str
        = "peter parker picked a peck of pickled peppers";
    string w = "peck";
 
    countOccurrences(str, w);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.util.regex.*;
 
class GFG {
 
  // Function to count total occurrences
  // of word "w" in String "str"
  static void countOccurrences(String str, String w)
  {
     
    // Get the regex to be checked
    String regexPattern = "\\b" + w + "\\b";
 
    Pattern pattern = Pattern.compile(regexPattern);
     
    // Variable to count total
    // occurrences of the given word
    int count = 0;
 
    while (str.contains(w)) {
      str = str.replace(w, "");
      // Increment count
      count++;
    }
 
    // Print the occurrences of the word
    System.out.print(count + "\n");
 
    return;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Input
    String str = "peter parker picked a peck of pickled peppers";
    String w = "peck";
 
    countOccurrences(str, w);
  }
}
 
// This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG {
 
  // Function to count total occurrences
  // of word "w" in String "str"
  static void countOccurrences(String str, String w)
  {
 
    // Get the regex to be checked
    String regexPattern = "\\b" + w + "\\b";
 
    Regex rx = new Regex(regexPattern,RegexOptions.Compiled | RegexOptions.IgnoreCase);
    // Variable to count total
    // occurrences of the given word
    int count = 0;
 
    while (str.Contains(w)) {
      str = str.Replace(w, "");
      // Increment count
      count++;
    }
 
    // Print the occurrences of the word
    Console.Write(count + "\n");
 
    return;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Input
    String str = "peter parker picked a peck of pickled peppers";
    String w = "peck";
 
    countOccurrences(str, w);
  }
}
 
// This code is contributed by gauravrajput1


输出
1

时间复杂度: O(N)
辅助空间: O(N)