Learning Goals In this lab, you will Use the for OBJ in CONTAINER loop to go over characters in the string Use if statements to compare the strings and characters Use if statements to detect when to output a singular noun Create and increment variables to count (use the accumulator pattern) Use string concatenation to create a new string to assemble the requested output Instructions Write a program that asks the user for a line of text and counts how many spaces, periods, or commas the text contains using the specified output format. Examples If the input is: Hello, world. the output is: There are 1 space, 1 period, and 1 comma. If the input is: Listen, Mr. Jones, calm down. the output is: There are 4 spaces, 2 periods, and 2 commas. Hints Review Figure 6.5.2 to see how to iterate over characters. Go over the string, character by character, and increment the counters for the requested characters. You will need a separate counter for every symbol that you are counting. Do not forget - initially, we do not have any characters counted, so the counter should be 0. You will need a separate check for each symbol to know whether to print its counter with a singular or a plural noun

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 8PE
icon
Related questions
Question

Learning Goals

In this lab, you will

  • Use the for OBJ in CONTAINER loop to go over characters in the string
  • Use if statements to compare the strings and characters
  • Use if statements to detect when to output a singular noun
  • Create and increment variables to count (use the accumulator pattern)
  • Use string concatenation to create a new string to assemble the requested output

Instructions

Write a program that asks the user for a line of text and counts how many spaces, periods, or commas the text contains using the specified output format.

Examples

If the input is:

Hello, world.

the output is:

There are 1 space, 1 period, and 1 comma.

If the input is:

Listen, Mr. Jones, calm down.

the output is:

There are 4 spaces, 2 periods, and 2 commas.

Hints

  • Review Figure 6.5.2 to see how to iterate over characters.
  • Go over the string, character by character, and increment the counters for the requested characters.
  • You will need a separate counter for every symbol that you are counting.
  • Do not forget - initially, we do not have any characters counted, so the counter should be 0.
  • You will need a separate check for each symbol to know whether to print its counter with a singular or a plural noun
LAB
ACTIVITY
6.18.1: LAB: Count spaces, periods, or commas
1 user_text=input()
2
3 num_spaces = ...
4 num_commas
5 num periods.
6
7 # TODO: for Loop to go over each character in user_text
8
9
10 output = f"There are "
11 if num_spaces == 1:
12
output += f"1 space,
output += f"{num_spaces} spaces,
main.py
13 else:
14
15
16
17 # TODO: finish the concatenation of the output string
9/14
Load default template...
Transcribed Image Text:LAB ACTIVITY 6.18.1: LAB: Count spaces, periods, or commas 1 user_text=input() 2 3 num_spaces = ... 4 num_commas 5 num periods. 6 7 # TODO: for Loop to go over each character in user_text 8 9 10 output = f"There are " 11 if num_spaces == 1: 12 output += f"1 space, output += f"{num_spaces} spaces, main.py 13 else: 14 15 16 17 # TODO: finish the concatenation of the output string 9/14 Load default template...
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT