Program to count the occurrences of each word in a given string sentence

Program to count the total number of words in a string

Explanation

In this program, we need to count the words present in the string.

Total number of words present in the string is 7.

Algorithm

  1. Define a string.
  2. To counts the words present in the string, we will iterate through the string and count the spaces present in the string. As each word always ends with a space.
  3. If a string starts with a space, then we must not count the first space as it is not preceded by a word.
  4. To count the last word, we will increment the count by 1.

Solution

Python

Output:

Total number of words in the given string: 7

Output:

Total number of words in the given string: 7

JAVA

Output:

Total number of words in the given string: 7 

C#

Output:

Total number of words in the given string: 7

PHP

Output:

Total number of words in the given string: 7

count() method only requires a single parameter for execution. However, it also has two optional parameters:

  • substring - string whose count is to be found.
  • start (Optional) - starting index within the string where search starts.
  • end (Optional) - ending index within the string where search ends.

Note: Index in Python starts from 0, not 1.


count() Return Value

count() method returns the number of occurrences of the substring in the given string.


Example 1: Count number of occurrences of a given substring

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)

Output

The count is: 2

Example 2: Count number of occurrences of a given substring using start and end

# define string
string = "Python is awesome, isn't it?"
substring = "i"

# count after first 'i' and before the last 'i'

count = string.count(substring, 8, 25)

# print count print("The count is:", count)

Output

The count is: 1

Here, the counting starts after the first

string.count(substring, start=..., end=...)
0 has been encountered, i.e.
string.count(substring, start=..., end=...)
1 index position.

In this post, we will see how to count the occurrences of each word in a given string sentence.
Algorithm
Step 1: Declare a String and store it in a variable.
Step 2: Declare a variable wordCount and initialise it to 0.
Step 3: Split the string using split(“ ”) function with space as the parameter and store all the words in a list.
Step 4: Use a for loop to iterate over the words in the list. While iterating check if the word in the list matches the word given by the user and if match found then increment the value of wordCount variable by 1.
Step 5: After completing the for loop print the value of wordCount variable.
Step 6: End.
Example

Input: "This is a sample Python program, Welcome to World Of Python Programming!"
Output: Number of occurrences found in the string: 2

Program

string="This is a sample Python program, Welcome to World Of Python Programming!"
word="Python"
list=[]
wordCount=0
list=string.split(" ")
for i in range(0,len(list)):
      if(word==list[i]):
            wordCount=wordCount+1
print("Number of occurrences found in the string:")
print(wordCount)

Output

Number of occurrences found in the string:
2

Source Code

str = "To change the overall look your document. To change the look available in the gallery"
c = dict()
txt = str.split(" ")
for t in txt:
	if t in c:
		c[t] += 1
	else:
		c[t] = 1
print(c)

Output

{'To': 2, 'change': 2, 'the': 3, 'overall': 1, 'look': 2, 'your': 1, 'document.': 1, 'available': 1, 'in': 1, 'gallery': 1}


To download raw file Click Here

You are given a string and a word your task is that count the number of occurrences of the given word in the string and print the number of occurrences of the word. 

Examples: 

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

Approach:

  • First, we split the string by spaces in a
  • Then, take a variable count = 0 and in every true condition we increment the count by 1
  • Now run a loop at 0 to length of string and check if our string is equal to the word
  • if condition is true then we increment the value of count by 1 and in the end, we print the value of count.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Below is the implementation of the above approach : 

C++




// C++ program to count the number

// of occurrence of a word in

// the given string

#include <bits/stdc++.h>

using namespace std;

 

1
0
1
1
1
2
1
3

1
4
1
5

1
6

1
7
1
2
1
9

 

1
7
1
1

1
7
1
3

 

1
7
1
5
1
6
1
7
1
8
1
9

1
7// C++ program to count the number1 // C++ program to count the number2

1
7
1
6

// C++ program to count the number5// C++ program to count the number6

// C++ program to count the number5

1
5
1
6// of occurrence of a word in0
1
8
1
9

1
7// of occurrence of a word in4

 

1
7// of occurrence of a word in6

1
7
1
0 // of occurrence of a word in9

1
7// the given string1 // the given string2// C++ program to count the number16 // C++ program to count the number12// C++ program to count the number16 // C++ program to count the number19// C++ program to count the number00
1
9// C++ program to count the number16// C++ program to count the number23

How do you find the number of occurrences of a word in a string?

Approach:.
First, we split the string by spaces in a..
Then, take a variable count = 0 and in every true condition we increment the count by 1..
Now run a loop at 0 to length of string and check if our string is equal to the word..

How do you count occurrences of each word in a string in Python?

The count() method returns the number of occurrences of a substring in the given string..
substring - string whose count is to be found..
start (Optional) - starting index within the string where search starts..
end (Optional) - ending index within the string where search ends..

How to count each word in string Java?

You can count words in Java String by using the split() method of String. A word is nothing but a non-space character in String, which is separated by one or multiple spaces. By using a regular expression to find spaces and split on them will give you an array of all words in a given String.

How to count the occurrences of a word in a text file Python?

To count the number of occurrences of a specific word in a text file, read the content of text file to a string and use String. count() function with the word passed as argument to the count() function.