编制求某字符串中数字字符个数的子程序以及调用它的主程序
时间: 2024-06-13 20:05:52 浏览: 149
在一个字符串中查找子串的个数
5星 · 资源好评率100%
以下是汇编语言实现求某字符串中数字字符个数的子程序以及调用它的主程序的代码:
```
.model small
.stack 100h
.data
msg db 'Enter a string: $'
count db 0
result db 'The number of digits in the string is: $'
.code
main proc
mov ax, @data
mov ds, ax
lea dx, msg
mov ah, 9
int 21h
lea dx, str
mov ah, 0Ah
int 21h
mov si, offset str + 2
mov cx, [offset str + 1]
mov bl, 0
call count_digits
lea dx, result
mov ah, 9
int 21h
mov dl, count
add dl, 48
mov ah, 2
int 21h
mov ah, 4ch
int 21h
count_digits proc
mov ah, [si]
cmp ah, 0
je done
cmp ah, 48
jl not_digit
cmp ah, 57
jg not_digit
inc bl
not_digit:
inc si
loop count_digits
done:
mov count, bl
ret
count_digits endp
str db 20, 0
main endp
end main
```
以下是Python实现求某字符串中数字字符个数的函数以及调用它的代码:
```python
def count_digits(s):
count = 0
for c in s:
if c.isdigit():
count += 1
return count
s = input("Enter a string: ")
result = "The number of digits in the string is: " + str(count_digits(s))
print(result)
```
阅读全文