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 Lazyroar for Lazyroar" Output: number of spaces = 5 Input: "neveropen" 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
# create function that # return space count def check_space(string): # counter count = 0 # loop for search each index for i in range ( 0 , len (string)): # Check each char # is blank or not if string[i] = = " " : count + = 1 return count # driver node string = "Welcome to neveropen" # call the function and display print ( "number of spaces " ,check_space(string)) |
Output:
number of spaces 2
Example 2:
Python3
# create function that # return space count def check_space(string): # counter count = 0 # loop for search each index for i in string: # Check each char # is blank or not if i = = " " : count + = 1 return count # driver node string = "Welcome to neveropen, Geeks!" # call the function and display print ( "number of spaces " ,check_space(string)) |
Output:
number of spaces 3
Example 3: Using the count() function.
Python3
# Create function that # return space count def check_space(Test_string): return Test_string.count( " " ) # Driver function if __name__ = = "__main__" : Test_string = "Welcome to neveropen, Geeks!" # Call the function and display 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
#Program for counting number of spaces in a string string = "Welcome to neveropen" c = 0 for i in string: if (i.isspace()): c + = 1 print ( "Number of Spaces : " + str (c)) |
Number of Spaces : 2
Example 5: Using Counter() function
Python3
# create function that # return space count from collections import Counter def check_space(string): freq = Counter(string) return freq[ ' ' ] # driver node string = "Welcome to neveropen" # call the function and display print ( "number of spaces " , check_space(string)) |
number of spaces 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Example #6: Using operator.countOf() method:
Python3
# Create function that # return space count import operator as op def check_space(Test_string): return op.countOf(Test_string, " " ) # Driver function if __name__ = = "__main__" : Test_string = "Welcome to neveropen, Geeks!" # Call the function and display print (f "Number of Spaces: {check_space(Test_string)}" ) |
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 ) # driver node string = "Welcome to neveropen" # call the function and display print ( "number of spaces " , check_space(string, 0 , 0 )) #This Code is contributed Vinay Pinjala. |
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 # Function that uses reduce() to count the number of spaces in a string def check_space_reduce(Test_string): # Use the reduce function to iterate through the string # and count the number of spaces return reduce ( lambda x, y: x + ( 1 if y = = " " else 0 ), Test_string, 0 ) # Main function that calls check_space_reduce() if __name__ = = "__main__" : # Test string Test_string = "Welcome to neveropen" # Call the function and display the result print (f "Number of Spaces: {check_space_reduce(Test_string)}" ) #This code is contributed by Jyothi pinjala. |
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.