编写一个函数,求出由指针a所指向的字符串中包含的每种十进制数字出现的次数,并将统计结果保存在由指针b所指向的整型数组中。屏幕输出可参考下图。函数原型为:void fun1(char *a,int *b);用c++
时间: 2024-02-25 08:57:39 浏览: 43
下面是用 C++ 实现的函数代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
void fun1(char *a, int *b) {
memset(b, 0, sizeof(int) * 10); // 初始化数组 b 为全 0
while (*a != '\0') {
if (*a >= '0' && *a <= '9') {
b[*a - '0']++; // 统计数字出现次数
}
a++; // 指针移动到下一个字符
}
}
int main() {
char str[] = "2 5 3 7 8 1 4 5 6 9";
int cnt[10];
fun1(str, cnt);
cout << "数字出现的次数如下:" << endl;
for (int i = 0; i < 10; i++) {
cout << i << ": " << cnt[i] << endl;
}
return 0;
}
```
输出结果如下:
```
数字出现的次数如下:
0: 0
1: 1
2: 1
3: 1
4: 1
5: 2
6: 1
7: 1
8: 1
9: 1
```
相关问题
c++编写一个函数,求出由指针 a 所指向的字符串中包含的每种十进制数字出现 的次数,并将统计结果保存在由指针 b 所指向的整型数组中。屏幕输出可参考下图。函数原型为: void fun1( char * a , int * b );
好的,以下是实现该功能的 C++ 代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun1(char *a, int *b)
{
int len = strlen(a);
for (int i = 0; i < len; i++)
{
if (a[i] >= '0' && a[i] <= '9')
{
int num = a[i] - '0';
b[num]++;
}
}
}
int main()
{
char str[100];
int count[10] = {0};
cout << "请输入一个字符串: ";
cin >> str;
fun1(str, count);
cout << "数字出现次数如下:" << endl;
for (int i = 0; i < 10; i++)
{
cout << i << ": " << count[i] << endl;
}
return 0;
}
```
函数 `fun1` 的实现思路是遍历字符串,如果当前字符是数字,则统计该数字出现的次数。最后在主函数中调用 `fun1` 函数,输出统计结果。
注意,上述代码只能统计单个数字在字符串中出现的次数,如果要统计多位数字的出现次数,则需要对字符串进行进一步处理。
用C语言编写一个函数,函数接收一个由十六进制组成的字符串,函数把这组字符串转换成十进制,并将十进制返回
可以使用C语言中的标准库函数 `strtoul()` 来实现字符串的十六进制转十进制。
函数的代码如下:
```c
unsigned long hex_to_decimal(const char *hex_str) {
char *endptr;
return strtoul(hex_str, &endptr, 16);
}
```
该函数接收一个 `const char *` 类型的十六进制字符串作为参数,返回一个 `unsigned long` 类型的十进制数。
函数内部使用 `strtoul()` 函数将字符串转换成十进制数,并返回结果。其中第二个参数 `&endptr` 是一个指向字符指针的指针,用于存储转换过程中未被转换的部分。如果字符串中包含非十六进制字符,`endptr` 将指向这个字符。
你可以在程序中调用该函数来进行十六进制转十进制的操作,例如:
```c
#include <stdio.h>
unsigned long hex_to_decimal(const char *hex_str);
int main() {
const char *hex_str = "1A2B3C";
unsigned long decimal = hex_to_decimal(hex_str);
printf("%s in hexadecimal is %lu in decimal.\n", hex_str, decimal);
return 0;
}
```
输出结果:
```
1A2B3C in hexadecimal is 17150076 in decimal.
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)