我需要一个c语言函数代码,输入一个字符串和一个字符,在字符串中的所有数字字符串前插入该字符并输出
时间: 2023-06-01 08:06:39 浏览: 59
可以使用以下代码实现:
```
#include <stdio.h>
void insert_char(char str[], char c) {
int i = 0, j;
while (str[i] != '\0') {
if (str[i] >= '0' && str[i] <= '9') {
j = i;
while (str[j] >= '0' && str[j] <= '9') {
j++;
}
str[j] = '\0';
while (j > i) {
str[j] = str[j - 1];
j--;
}
str[i] = c;
}
i++;
}
printf("%s\n", str);
}
int main() {
char str[100], c;
printf("请输入一个字符串和一个字符:\n");
scanf("%s %c", str, &c);
insert_char(str, c);
return 0;
}
```
这个函数会遍历字符串中的每个字符,如果当前字符是数字,就将其前面插入指定的字符。最后输出修改后的字符串。您可以根据需要调整函数的输入和输出方式。