输入一个字符串,统计字符串中十六进制字符个数并输出。十六进制字符包括0~9,A~F,a~f。【输入格式】 在一行中输入不超过80个字符长度的,以回车结束的非空字符串。【输出格式】 在一行中输出字符串中十六进制字符的个数。【输入样例】 Hello123!@#【输出样例】4 【函数定义要求】 请在该程序文件中,实现函数intfhex(charstrll),统计字符串str中十六进制字符的个数,并作为函数的返回结果。#include <stdio.h> int fhex(char str[]); int main(void) { char str[80]; gets(str); printf("%d",fhex(str)); return 0; }
时间: 2024-03-08 17:48:06 浏览: 22
以下是代码实现:
```c
#include <stdio.h>
int fhex(char str[]);
int main(void) {
char str[80];
gets(str);
printf("%d", fhex(str));
return 0;
}
int fhex(char str[]) {
int i, count = 0;
for(i = 0; str[i] != '\0'; i++) {
if((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'F') || (str[i] >= 'a' && str[i] <= 'f')) {
count++;
}
}
return count;
}
```
相关问题
编写汇编语言程序实现键盘输入字符串(最多64位),统计其中数字的个数,并在屏幕上以十六进制的形式输出
以下是汇编语言程序的实现:
```
section .data
input db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
output db "Number of digits: ", 0
hex_output db "0x", 0, 0, 0, 0
newline db 0Ah, 0
section .text
global _start
_start:
; Read input from keyboard
mov eax, 3 ; System call for reading input
mov ebx, 0 ; File descriptor for keyboard input
mov ecx, input ; Address of input buffer
mov edx, 64 ; Maximum number of characters to read
int 0x80
; Count the number of digits
xor eax, eax
xor ebx, ebx
loop_start:
mov bl, byte [input + eax] ; Load next character
cmp bl, 0 ; End of string?
je end_loop
cmp bl, '0' ; Is it a digit?
jl not_digit
cmp bl, '9'
jg not_digit
inc ebx ; Increment digit count
not_digit:
inc eax ; Move to next character
jmp loop_start
end_loop:
; Convert digit count to hex and print to screen
mov eax, ebx
mov ebx, 16
div ebx ; Divide by 16
add edx, '0' ; Convert remainder to ASCII
cmp edx, '9'
jle skip_add ; Skip if remainder is less than 10
add edx, 7 ; Add offset for hex digits A-F
skip_add:
mov byte [hex_output + 2], dl ; Store least significant digit
mov ecx, hex_output ; Address of output buffer
mov eax, 4 ; System call for printing output
mov ebx, 1 ; File descriptor for screen output
mov edx, 4 ; Number of characters to print
int 0x80
; Print newline character to screen
mov eax, 4 ; System call for printing output
mov ebx, 1 ; File descriptor for screen output
mov ecx, newline
mov edx, 2 ; Number of characters to print
int 0x80
; Exit program
mov eax, 1 ; System call for exiting program
xor ebx, ebx ; Exit status code
int 0x80
```
此程序使用系统调用来读取键盘输入,统计数字的数量并将其转换为十六进制格式输出。请注意,此程序仅适用于 x86 架构的 Linux 系统。
汇编语言编写完整程序实现下述功能:键盘输入字符串(最多100个),统计其中数字的个数,将结果在屏幕上以十六进制的形式显示。
好的,以下是汇编语言程序,实现了您的要求:
```
; 定义常量
MAX_LEN equ 100 ; 最大输入长度
CR equ 0AH ; 换行符的 ASCII 码
; 定义变量
input db MAX_LEN+2 ; 存储输入的字符串,加2是为了留出两个字节存储长度和换行符
count dw 0 ; 计数器,用来统计数字的个数
; 代码段
org 100h
start:
; 初始化数据段
mov ax, cs
mov ds, ax
mov es, ax
; 输出提示信息
mov ah, 09h
mov dx, offset prompt
int 21h
; 读取用户输入
mov ah, 0Ah
mov dx, offset input
int 21h
; 统计数字个数
mov si, offset input+2 ; 跳过输入字符串的长度和换行符
mov cx, word ptr [input] ; 读取输入字符串的长度
mov bx, 0 ; 清零计数器
check_digit:
cmp byte ptr [si], 30h ; 检查当前字符是否为数字字符 '0'
jb not_digit ; 如果小于 '0',则跳过
cmp byte ptr [si], 39h ; 检查当前字符是否为数字字符 '9'
ja not_digit ; 如果大于 '9',则跳过
inc bx ; 如果是数字字符,则计数器加一
not_digit:
inc si ; 指向下一个字符
loop check_digit ; 继续检查下一个字符
; 将数字个数以十六进制形式输出
mov ah, 02h
mov dl, '0'
int 21h
mov dl, 'x'
int 21h
mov ax, count ; 将计数器的值存入 AX 中
mov si, offset hex_table ; 十六进制字符表的偏移地址
mov cx, 4 ; 需要输出四个字符
print_hex:
rol ax, 4 ; 将 AX 中的值向左循环移位四位
mov dl, byte ptr [si+ax] ; 从十六进制字符表中取出对应的字符
int 21h
loop print_hex ; 继续输出下一个字符
; 换行
mov dl, CR
int 21h
; 结束程序
mov ah, 4Ch
xor al, al
int 21h
; 数据段
prompt db 'Please input a string (max length: 100): $'
hex_table db '0123456789ABCDEF'
; 结束标志
times 510-($-$$) db 0
dw 0xAA55
```
这个程序首先输出提示信息,然后读取用户输入的字符串,接着统计其中数字的个数,最后将数字个数以十六进制形式输出。您可以使用 DOSBox 等模拟器来运行这个程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)