Length and spaces of a line

Programs often have to count things in text typed by the user. Each key is a code, so a comparison with the code of a space finds the spaces.

What to build

Read one line and print how many characters it has, and how many of them are spaces.

Requirements

  • The length does not include the Enter key.
  • Print the length, a space, and then the number of spaces.
  • Print each number in decimal, without leading zeros.

Input

One line of 1 to 40 characters, then Enter.

Output

Two numbers separated by one space, for example 11 1 for hello world.

Constraints

The line has no tab characters. The numbers are below 100.

Example

Input:

hello world

Output:

11 1

Why it works

A loop that reads a key, tests whether it is Enter, and otherwise counts it, handles any length. AAM turns a number below 100 into its tens digit in AH and its ones digit in AL, so the print code does not need a division loop. The tens digit is skipped when it is 0, so that 5 is not printed as 05.

Examples

Example 1 · Input
hello world
Expected output
11 1
Example 2 · Input
a b c d e
Expected output
9 4

Hints

Hint 1

Read the keys one at a time with AH = 1 until you get Enter (0Dh). Keep the length in BL and the spaces in BH.

Hint 2

Compare each key with ' ' (20h) and add 1 to BH when it matches.

Hint 3

Print each count below 100 with AAM: AH is the tens and AL the ones. Skip the tens digit when it is 0. A procedure that prints AL avoids writing the code twice.

Solution

Show one correct solution (try it yourself first)
; Length and spaces of a line
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
    MOV BL, 0           ; the length
    MOV BH, 0           ; the spaces
READ:
    MOV AH, 1
    INT 21H
    CMP AL, 0DH
    JE REPORT
    INC BL
    CMP AL, ' '
    JNE READ
    INC BH
    JMP READ
REPORT:
    MOV AL, BL
    CALL SHOWNUM
    MOV DL, ' '
    MOV AH, 2
    INT 21H
    MOV AL, BH
    CALL SHOWNUM
    MOV AH, 4CH
    INT 21H
MAIN ENDP

SHOWNUM PROC            ; prints AL (0 to 99) in decimal
    PUSH BX             ; keep the caller's counters
    MOV AH, 0
    AAM                 ; AH = tens, AL = ones
    MOV BX, AX
    CMP BH, 0
    JE ONES
    MOV DL, BH
    ADD DL, '0'
    MOV AH, 2
    INT 21H
ONES:
    MOV DL, BL
    ADD DL, '0'
    MOV AH, 2
    INT 21H
    POP BX
    RET
SHOWNUM ENDP
END MAIN

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Keyboard and screen with INT 21h →