编制求某字符串中数字字符个数的子程序以及调用它的主程序
时间: 2024-06-13 11:05:52 浏览: 14
以下是汇编语言实现求某字符串中数字字符个数的子程序以及调用它的主程序的代码:
```
.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)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)