Count occurrences of a word in string | Set 2 (Using Regular Expressions)
Given a string str and a word w, the task is to print the number of the occurrence of the given word in the string str using Regular Expression.
Examples:
Input: str = “peter parker picked a peck of pickled peppers”, w = “peck”
Output: 1
Explanation: There is only one occurrence of the word “peck” in the given string. Therefore, the output is 1.
Input: str = “How much wood would a woodchuck chuck if a woodchuck could chuck wood ?”, w = “wood”
Output: 2
Explanation: There are only two occurrences of the word “wood” in the given string.
Therefore, the output is 2.
Input: str = “She sells seashells by the seashore”, w = “sea”
Output: 0
Explanation: There is no occurrence of the word “sea” in the given string. Therefore, the output is 0.
Approach: The required regular expression to find the required count of string w in the given string is “\\bw\\b”, where \b is a word boundary. Follow the steps to solve the problem
regex = “\\bw\\b”
Below is the implementation of the above approach :
C++
#include <iostream>
#include <regex>
using namespace std;
void countOccurrences(string str, string w)
{
string regexPattern = "\\b" + w + "\\b" ;
const regex pattern(regexPattern);
int count = 0;
auto it
= sregex_iterator(str.begin(), str.end(), pattern);
for (it; it != sregex_iterator(); it++) {
count++;
}
cout << count << endl;
return ;
}
int main()
{
string str
= "peter parker picked a peck of pickled peppers" ;
string w = "peck" ;
countOccurrences(str, w);
return 0;
}
|
Java
import java.util.*;
import java.util.regex.*;
class GFG {
static void countOccurrences(String str, String w)
{
String regexPattern = "\\b" + w + "\\b" ;
Pattern pattern = Pattern.compile(regexPattern);
int count = 0 ;
while (str.contains(w)) {
str = str.replace(w, "" );
count++;
}
System.out.print(count + "\n" );
return ;
}
public static void main(String[] args)
{
String str = "peter parker picked a peck of pickled peppers" ;
String w = "peck" ;
countOccurrences(str, w);
}
}
|
Python3
import re
def countOccurrences( str ,w):
regexPattern = "\\b" + w + "\\b"
count = 0
for m in re.finditer(regexPattern, str ):
count = count + 1
print (count)
str = "peter parker picked a peck of pickled peppers"
w = "peck"
countOccurrences( str ,w)
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
static void countOccurrences(String str, String w)
{
String regexPattern = "\\b" + w + "\\b" ;
Regex rx = new Regex(regexPattern,RegexOptions.Compiled | RegexOptions.IgnoreCase);
int count = 0;
while (str.Contains(w)) {
str = str.Replace(w, "" );
count++;
}
Console.Write(count + "\n" );
return ;
}
public static void Main(String[] args)
{
String str = "peter parker picked a peck of pickled peppers" ;
String w = "peck" ;
countOccurrences(str, w);
}
}
|
Javascript
function countOccurrences(string, substring) {
var n = 0;
var position = 0;
while ( true ) {
position = string.indexOf(substring, position);
if (position != -1) {
n++;
position += substring.length;
} else {
break ;
}
}
return n;
}
console.log(countOccurrences( "peter parker picked a peck of pickled peppers" , "peck" ));
|
Time Complexity : O(N)
Auxiliary Space : O(N)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...