PythonServer Side ProgrammingProgramming Show In this article, we will learn about the solution to the problem statement given below. Problem statement − We are given a string we need to count the number of words in the string Approach 1 − Using split() functionSplit function breaks the string into a list iterable with space as a delimiter. if the split() function is used without specifying the delimiter space is allocated as a default delimiter. ExampleLive Demo test_string = "Tutorials point is a learning platform" #original string print ("The original string is : " + test_string) # using split() function res = len(test_string.split()) # total no of words print ("The number of words in string are : " + str(res))OutputThe original string is : Tutorials point is a learning platform The number of words in string are : 6Approach 2 − Using regex moduleHere findall() function is used to count the number of words in the sentence available in a regex module. ExampleLive Demo import re test_string = "Tutorials point is a learning platform" # original string print ("The original string is : " + test_string) # using regex (findall()) function res = len(re.findall(r'\w+', test_string)) # total no of words print ("The number of words in string are : " + str(res))OutputThe original string is: Tutorials point is a learning platform The number of words in string is: 6 Approach 3 − Using sum()+ strip()+ split() functionHere we first check all the words in the given sentence and add them using the sum() function. ExampleLive Demo import string test_string = "Tutorials point is a learning platform" # printing original string print ("The original string is: " + test_string) # using sum() + strip() + split() function res = sum([i.strip(string.punctuation).isalpha() for i in test_string.split()]) # no of words print ("The number of words in string are : " + str(res))OutputThe original string is : Tutorials point is a learning platform The number of words in string are : 6All the variables are declared in the local scope and their references are seen in the figure above. ConclusionIn this article, we have learned how to count the number of words in the sentence.
Updated on 11-Jul-2020 11:33:33
Given a string, count number of words in it. The words are separated by following characters: space (‘ ‘) or new line (‘\n’) or tab (‘\t’) or a combination of these. There can be many solutions to this problem. Following is a simple and interesting solution. The idea is to maintain two states: IN and OUT. The state OUT indicates that a separator is seen. State IN indicates that a word character is seen. We increment word count when previous state is OUT and next character is a word character.
Time complexity: O(n) This article is compiled by Aarti_Rathi and Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Method 2: using String.split() method
Below is the implementation of the above approach:
Time Complexity: O(N) Method 3: using StringTokenizer.countTokens() method
Below is the implementation of the above approach:
Time Complexity: O(N) Method 4: using Character.isLetter() method
Below is the implementation of the above approach:
Time Complexity: O(N) Auxiliary Space: O(1) Article Tags : Practice Tags : |