Program to Count Occurrence of Word in Given String
- Write a program to Count Occurrence of Word in Given String in C
- Write a program to Count Occurrence of Word in Given String in C++
- Write a program to Count Occurrence of Word in Given String in Python
- Write a program to Count Occurrence of Word in Given String in PHP
- Write a program to Count Occurrence of Word in Given String in Java
- Write a program to Count Occurrence of Word in Given String in Java Script
- Write a program to Count Occurrence of Word in Given String in C#
Explanation:
One basic method for counting the occurrence of a word in a given string is to divide the string into words and count the number of times the word appears.
Logic Explanation:
- Split the String into Words:
- First, split the input string into words. This can be done using spaces or other delimiters (such as punctuation) depending on the definition of “word” in the context.
- Iterate Through the Words:
- Once the string is split into words, iterate over the list of words.
- Count Occurrences:
- Compare each word with the target word and count how many times it matches.
- Return the Count:
- Finally, return the total count of the word’s occurrences in the string.
Program to Count Occurrence of Word in Given String
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
#include <string.h>
int countWordOccurrences(char *str, char *word) {
int count = 0;
char *pos = strstr(str, word);
while (pos != NULL) {
// Check if the match is a complete word
if ((pos == str || *(pos - 1) == ' ') && (*(pos + strlen(word)) == ' ' || *(pos + strlen(word)) == '\0')) {
count++;
}
pos = strstr(pos + 1, word);
}
return count;
}
int main() {
char str[200], word[50];
printf("Enter the main string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
printf("Enter the word to count: ");
fgets(word, sizeof(word), stdin);
word[strcspn(word, "\n")] = '\0';
int count = countWordOccurrences(str, word);
printf("The word '%s' occurred %d times.\n", word, count);
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int countWordOccurrences(const string &str, const string &word) {
int count = 0;
istringstream stream(str);
string token;
while (stream >> token) {
if (token == word) {
count++;
}
}
return count;
}
int main() {
string str, word;
cout << "Enter the main string: ";
getline(cin, str);
cout << "Enter the word to count: ";
cin >> word;
int count = countWordOccurrences(str, word);
cout << "The word '" << word << "' occurred " << count << " times." << endl;
return 0;
}
def count_word_occurrences(string, word):
words = string.split()
return words.count(word)
string = input("Enter the main string: ")
word = input("Enter the word to count: ")
count = count_word_occurrences(string, word)
print(f"The word '{word}' occurred {count} times.")
<?php
function countWordOccurrences($string, $word) {
$words = explode(" ", $string);
return count(array_filter($words, fn($w) => $w === $word));
}
echo "Enter the main string: ";
$string = trim(fgets(STDIN));
echo "Enter the word to count: ";
$word = trim(fgets(STDIN));
$count = countWordOccurrences($string, $word);
echo "The word '$word' occurred $count times.\n";
?>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the main string: ");
String mainString = scanner.nextLine();
System.out.print("Enter the word to count: ");
String word = scanner.next();
int count = countWordOccurrences(mainString, word);
System.out.println("The word '" + word + "' occurred " + count + " times.");
}
public static int countWordOccurrences(String str, String word) {
String[] words = str.split("\\s+");
int count = 0;
for (String w : words) {
if (w.equals(word)) {
count++;
}
}
return count;
}
}
function countWordOccurrences(string, word) {
const words = string.split(" ");
return words.filter(w => w === word).length;
}
const string = prompt("Enter the main string:");
const word = prompt("Enter the word to count:");
const count = countWordOccurrences(string, word);
console.log(`The word '${word}' occurred ${count} times.`);
using System;
class Program {
static void Main() {
Console.Write("Enter the main string: ");
string mainString = Console.ReadLine();
Console.Write("Enter the word to count: ");
string word = Console.ReadLine();
int count = CountWordOccurrences(mainString, word);
Console.WriteLine($"The word '{word}' occurred {count} times.");
}
static int CountWordOccurrences(string str, string word) {
string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int count = 0;
foreach (string w in words) {
if (w == word) {
count++;
}
}
return count;
}
}