用 c 语言写以下题目:编写一个程序,从键盘上读入一个数字串,把数字转化为对应的小写英语数字单词输出。例如:输入234,输出two three four。
时间: 2024-03-26 18:35:02 浏览: 27
以下是用 C 语言编写的程序:
```c
#include <stdio.h>
int main() {
char *digits[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; // 存储数字对应的英文单词
char str[100]; // 存储输入的数字串
printf("请输入数字串:");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i++) {
int digit = str[i] - '0'; // 将字符转换为数字
printf("%s ", digits[digit]); // 输出对应的英文单词
}
return 0;
}
```
输入数字串后,程序会遍历每个字符,将其转换为对应的数字,然后输出数字对应的英文单词。
相关问题
用c语言编写一个程序,从键盘上读入一个数字串,把数字转化为对应的小写英语数字单词输出。例如:输入234,输出two three four。 输入:一串数字 输出:用空格隔开英文数字单词(英文字母都是小写)。 样例: 234 two three four
以下是C语言实现的代码:
```c
#include <stdio.h>
#include <string.h>
char* num2word(char c){
switch(c){
case '0': return "zero";
case '1': return "one";
case '2': return "two";
case '3': return "three";
case '4': return "four";
case '5': return "five";
case '6': return "six";
case '7': return "seven";
case '8': return "eight";
case '9': return "nine";
default: return "";
}
}
int main(){
char num[100];
scanf("%s", num);
for(int i = 0; i < strlen(num); i++){
printf("%s ", num2word(num[i]));
}
return 0;
}
```
代码思路如下:
1. 定义一个函数 `num2word`,它根据输入的数字字符返回对应的英文单词。例如,当输入字符'1'时,该函数返回字符串"one"。
2. 从键盘上读入一个数字串。
3. 对于数字串中的每个字符,调用 `num2word` 函数并输出对应的英文单词,并在单词之间添加一个空格。
4. 完成程序的执行。
输入示例:
```
234
```
输出示例:
```
two three four
```
用C语言写一个程序:从键盘输入字符串,统计字符串中数字字符出现的次数并输出
下面是一个简单的 C 语言程序,可以实现从键盘输入字符串,统计字符串中数字字符出现的次数并输出:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, count = 0;
printf("请输入字符串:");
scanf("%s", str);
for (i = 0; i < strlen(str); i++) {
if (str[i] >= '0' && str[i] <= '9') {
count++;
}
}
printf("字符串中数字字符出现的次数为:%d\n", count);
return 0;
}
```
程序首先声明一个字符数组 `str`,用于存储输入的字符串。然后使用 `scanf` 函数从键盘读入字符串,存储到 `str` 数组中。
接着使用 `for` 循环遍历字符串中的每个字符,如果当前字符是数字字符,则将计数器 `count` 加 1。
最后输出计数器 `count` 的值,即字符串中数字字符出现的次数。
需要注意的是,这个程序只能统计 ASCII 码表中的数字字符,如果字符串中包含其他编码的数字字符,可能无法正确统计。同时,这个程序也没有对字符串长度进行限制,如果输入的字符串过长,可能会导致程序崩溃。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)