c语言编一函数,将一个字符数组中的数字字符存于另一个字符数组中。
时间: 2024-05-02 20:22:57 浏览: 16
好的,这个问题很简单,你可以尝试以下这段c语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void get_digits(char *str, char *result) {
int len = strlen(str);
int i, j = 0;
for (i = 0; i < len; i++) {
if (isdigit(str[i])) {
result[j++] = str[i];
}
}
result[j] = '\0'; // 添加结束符
}
int main() {
char str[100], result[100];
printf("请输入一行字符串:");
gets(str);
get_digits(str, result);
printf("结果: %s\n", result);
return 0;
}
```
以上代码中 `get_digits` 函数将输入字符串中的数字字符存入另一个字符数组当中。
相关问题
用c语言编一函数,将一个字符数组中的数字字符存于另一个字符数组中
好的,我理解了。下面是用C语言编写的函数将一个字符数组中的数字字符存于另一个字符数组中的代码:
```
void extract_digits(char* input, char* output){
int i,j = 0;
for(i = 0; input[i] != '\0'; i++){
if(isdigit(input[i])){
output[j] = input[i];
j++;
}
}
output[j] = '\0';
}
```
这个函数的作用是将输入的字符数组中的数字字符提取出来,存储到输出的字符数组中。它的实现是遍历输入数组,如果当前字符是数字,就将它存储到输出数组中,最后在输出数组末尾加上一个空字符。
你可以拿此代码进行测试,也可以将此代码复制到C语言的开发环境中进行编译和测试。
用C语言编一函数,将一个字符数组中的数字字符存于另一个字符数组中
好的,你的问题很简单。以下是一个将字符数组中的数字字符存储在另一个字符数组中的C语言函数:
```c
#include <ctype.h>
#include <string.h>
void extract_digits(char* input, char* output) {
int outputIndex = 0;
for (int i = 0; i < strlen(input); i++) {
if (isdigit(input[i])) {
output[outputIndex++] = input[i];
}
}
output[outputIndex] = '\0';
}
```
此函数名为 extract_digits,使用了 ctype.h 和 string.h 头文件,以检查输入数组中的每个字符是否为数字,并将其存储在单独的输出数组中。
建议在使用此函数之前要先了解C语言数组,循环语句,以及`isdigit()`函数的使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)