请编写函数 fun,函数的功能是:在字符串中的所有数字字符前加一个$字符。(C语言)
时间: 2023-06-13 12:09:16 浏览: 148
以下是函数fun的代码实现:
```c
void fun(char *str) {
int i = 0;
while (str[i] != '\0') { // 遍历整个字符串
if (str[i] >= '0' && str[i] <= '9') { // 判断是否为数字字符
int j = i;
while (str[j] != '\0') { // 将数字字符后面的字符全部后移一位
str[j + 1] = str[j];
j++;
}
str[i] = '$'; // 在数字字符前插入$字符
i += 2; // 跳过$字符和移动的字符
} else {
i++;
}
}
}
```
使用方法如下:
```c
#include <stdio.h>
void fun(char *str);
int main() {
char str[100];
printf("请输入字符串:");
gets(str);
fun(str);
printf("处理后的字符串为:%s", str);
return 0;
}
```
相关问题
c语言请编写函数fun,其功能是:统计s所指字符串中的数字字符个数,并作为函数值返回
下面是一个简单的 C 语言函数 fun,用于统计字符串中的数字字符个数:
```c
int fun(char *s) {
int count = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9') {
count++;
}
s++;
}
return count;
}
```
该函数通过遍历字符串中的每个字符,判断是否为数字字符,如果是,则计数器加一。最后返回计数器的值。
C语言函数fun的功能是:统计字符串中英文字母、空格、数字和其它字符的个数
以下是C语言函数fun的实现,可以统计字符串中英文字母、空格、数字和其它字符的个数:
```c
#include <stdio.h>
#include <ctype.h>
void fun(char *str, int *a, int *b, int *c, int *d) {
while (*str != '\0') {
if (isalpha(*str)) {
(*a)++;
} else if (*str == ' ') {
(*b)++;
} else if (isdigit(*str)) {
(*c)++;
} else {
(*d)++;
}
str++;
}
}
int main() {
char str[100];
int a = 0, b = 0, c = 0, d = 0;
printf("请输入一个字符串:");
gets(str);
fun(str, &a, &b, &c, &d);
printf("英文字母个数:%d\n", a);
printf("空格个数:%d\n", b);
printf("数字个数:%d\n", c);
printf("其它字符个数:%d\n", d);
return 0;
}
```