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
- Define a string.
- 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.
- If a string starts with a space, then we must not count the first space as it is not preceded by a word.
- 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: 2Example 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: 1Here, 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
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: 2Source 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 HereYou 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 TimeApproach:
- 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;
10 1112 13
1415
16
1712 19
1711
1713
171516171819
17// C++ program to count the number1 // C++ program to count the number2
1716
// C++ program to count the number5// C++ program to count the number6
// C++ program to count the number51516// of occurrence of a word in01819
17// of occurrence of a word in4
17// of occurrence of a word in6
1710 // of occurrence of a word in9
17// 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 number0019// C++ program to count the number16// C++ program to count the number23