Python program to count the number of spaces in string
Given a string, the task is to write a Python program to count the number of spaces in the string.
Examples:
Input: "my name is geeks for geeks"
Output: number of spaces = 5
Input: "geeksforgeeks"
Output: number of spaces=0
Approach:
- Input string from the user
- Initialize count variable with zero
- Run a for loop i from 0 till the length of the string
- Inside for loop, check if s[i] == blank, then increment count by 1
- Outside for loop, print count
Example 1:
Python3
def check_space(string):
count = 0
for i in range ( 0 , len (string)):
if string[i] = = " " :
count + = 1
return count
string = "Welcome to geeksforgeeks"
print ( "number of spaces " ,check_space(string))
|
Output:
number of spaces 2
Example 2:
Python3
def check_space(string):
count = 0
for i in string:
if i = = " " :
count + = 1
return count
string = "Welcome to geeksforgeeks, Geeks!"
print ( "number of spaces " ,check_space(string))
|
Output:
number of spaces 3
Example 3: Using the count() function.
Python3
def check_space(Test_string):
return Test_string.count( " " )
if __name__ = = "__main__" :
Test_string = "Welcome to geeksforgeeks, Geeks!"
print (f "Number of Spaces: {check_space(Test_string)}" )
|
Output:
Number of Spaces: 3
The time and space complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Example 4: Using isspace() function in python3
Python3
string = "Welcome to geeksforgeeks"
c = 0
for i in string:
if (i.isspace()):
c + = 1
print ( "Number of Spaces : " + str (c))
|
Output
Number of Spaces : 2
Example 5: Using Counter() function
Python3
from collections import Counter
def check_space(string):
freq = Counter(string)
return freq[ ' ' ]
string = "Welcome to geeksforgeeks"
print ( "number of spaces " , check_space(string))
|
Output
number of spaces 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Example #6: Using operator.countOf() method:
Python3
import operator as op
def check_space(Test_string):
return op.countOf(Test_string, " " )
if __name__ = = "__main__" :
Test_string = "Welcome to geeksforgeeks, Geeks!"
print (f "Number of Spaces: {check_space(Test_string)}" )
|
Output
Number of Spaces: 3
Time complexity: O(n)
Auxiliary Space: O(1)
Example #7: Using Recursion
Python3
def check_space(string,c,i):
if i = = len (string):
return c
if string[i] = = " " :
c + = 1
return check_space(string,c,i + 1 )
string = "Welcome to geeksforgeeks"
print ( "number of spaces " , check_space(string, 0 , 0 ))
|
Output
number of spaces 2
Time complexity: O(n)
Auxiliary Space: O(n)
Example #8: Using reduce():
1.Import the reduce function from the functools module.
2.Define a function named “check_space_reduce” that takes a string as input.
3.Within the function, use the reduce function to iterate through the string and count the number of spaces in the string.
4.The reduce function uses a lambda function that takes two arguments: the current count of spaces and the current character in the string. If the 5.current character is a space, the lambda function returns the current count plus 1, otherwise it returns the current count.
6.The reduce function initializes the count to 0 and iterates through the characters in the string, applying the lambda function at each step and 7.returning the final count of spaces.
8.Return the count of spaces from the function.
9.In the main function, define a test string.
10.Call the “check_space_reduce” function with the test string as an argument and store the result in a variable named “spaces”.
11.Print the number of spaces in the test string using an f-string.
Python3
from functools import reduce
def check_space_reduce(Test_string):
return reduce ( lambda x, y: x + ( 1 if y = = " " else 0 ), Test_string, 0 )
if __name__ = = "__main__" :
Test_string = "Welcome to geeksforgeeks"
print (f "Number of Spaces: {check_space_reduce(Test_string)}" )
|
Output
Number of Spaces: 2
The time complexity : O(n), where n is the length of the input string. This is because the reduce() function iterates through each character in the string exactly once and performs a constant amount of work for each character, and thus the time complexity is linear in the length of the input string.
The space complexity : O(1), as the only space used by the program is a constant amount of memory to store the variable “spaces”, which holds the count of spaces in the string. Since the memory usage does not depend on the size of the input string, the space complexity is constant or O(1). Note that the lambda function used inside the reduce() function does not create any additional data structures or use any additional memory that would affect the space complexity.
Last Updated :
22 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...