函数fun的原理是传字符串指针和字符串长度两个参数,用for循环遍历字符串,
当遇到数字字符时,就把数字字符和其后面的字符向后移动1个字符,
在原来数字字符的位置写一个'$',当前字符位置i加1,字符串长度n加1.
完整的C语言程序如下
#include<stdio.h>
#include<string.h>
void fun(char* s,int n){
int i,j;
for(i=0;s[i]!='\0';i++){
if('0'<=s[i]&&s[i]<='9'){
for(j=n;j>=i;j--){
s[j+1]=s[j];
}
s[i]='$';
i++;
n++;
}
}
}
int main(){
char a[80];
scanf("%s",a);
fun(a,strlen(a));
printf("%s\n",a);
return 0;
}
注意内循环增加一个$时,当前位置需要i++,长度n++。
#include "stdafx.h"
#include <iostream>
using namespace std;
#define N 100
void fun(char s[],int n)
{
for(int i=0;i<n;i++)
{
if (s[i] >= '0'&&s[i] <= '9')
{
for (int j = n; j >= i; j--)
s[j + 1] = s[j];
s[i] = '$';
n++;
i++;
}
}
cout << s << endl;
}
int main()
{
char s[N];
int i = 0;
cin >> s;
i = strlen(s);
fun(s,i);
system("pause");
return 0;
}