PracticeStrings
Count Characters in a File Record
Context
A file-ingestion tool receives one record as a string before parsing it. The record may contain letters, digits, punctuation, spaces, or other whitespace. Determine how many characters the tool received so it can track record sizes consistently.
Problem
Write a function that returns the number of characters in the supplied file record. Start a counter at zero and account for each character exactly once while traversing the record. Every character is ordinary content: spaces, tabs, and other whitespace must be included rather than removed or skipped. An empty record has a count of zero.
Examples
Example 1
Input:
record = "Hello"Output:
5Explanation: For the record `Hello`, the executed function returns 5, and its final counter value is 5.
Example 2
Input:
record = "Line one\tLine two"Output:
17Explanation: For the record `Line one\tLine two`, the executed function returns 17; the tab is included in that count.
Example 3
Input:
record = ""Output:
0Explanation: For the empty record, the executed function returns 0, with a final counter value of 0.
Constraints
0 <= len(record) <= 5000- Types:
recordis str; result is int
Function signature
def count_record_characters(record: str) -> int
recordstr- The complete file record whose characters must be counted.
- returns int
- The total number of characters in record, including whitespace.
Adapted from MBPP problem task_813 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The input is already provided as one string; no parsing or delimiter handling is needed.
Code
Saved