198,788 views
6 votes
6 votes
9.9 LAB: Count input length without spaces, periods, exclamation points, or commas

Given a line of text as input, output the number of characters excluding spaces, periods, exclamation points, or commas.

Ex: If the input is:

Listen, Mr. Jones, calm down.
the output is:

21

User Beder Acosta Borges
by
2.4k points

2 Answers

25 votes
25 votes

Final answer:

To count the number of characters in the input text excluding spaces, periods, exclamation points, or commas, you can use a loop to iterate through each character and increment a counter variable for non-excluded characters.

Step-by-step explanation:

To count the number of characters in the input text excluding spaces, periods, exclamation points, or commas, you can use a loop to iterate through each character of the input. Within the loop, you can check if the character is a space, period, exclamation point, or comma, and if it is not, increment a counter variable. At the end of the loop, the counter variable will contain the total count of characters.

Here is an example in Python:

def count_characters(text):
count = 0
for char in text:
if char not in [' ', '.', '!', ',']:
count += 1
return count

input_text = input()
character_count = count_characters(input_text)
print(character_count)

User Madkitty
by
2.4k points
22 votes
22 votes

Final answer:

To count the number of characters excluding spaces, periods, exclamation points, and commas in a line of text, you can use programming languages like Python.

Step-by-step explanation:

To count the number of characters excluding spaces, periods, exclamation points, and commas, you can use programming languages like Python. One way to accomplish this is by iterating through each character in the input text and checking if it is a space, period, exclamation point, or comma. If it is not, you increment a counter. Here is an example in Python:

text = input("Enter a line of text: ")
count = 0
for char in text:
if char not in [' ', '.', '!', ',']:
count += 1
print('The number of characters excluding spaces, periods, exclamation points, and commas is:', count)
User Ramon Smits
by
2.7k points