Header Ads

Header ADS

8 Design a program for creating a machine that counts the number of 1’s and 0’s in a given string.

 def count_ones_and_zeros(input_string):

    ones_count = 0

    zeros_count = 0

    for symbol in input_string:

        if symbol == '1':

            ones_count += 1

        elif symbol == '0':

            zeros_count += 1

    return ones_count, zeros_count

# Example usage:

input_strings = ["101", "0101", "1100", "111000", "000111", "1010", "1101"]

for input_str in input_strings:

    ones, zeros = count_ones_and_zeros(input_str)

    print(f'The string "{input_str}" has {ones} ones and {zeros} zeros.')



No comments

Powered by Blogger.