将数字字符后面插入*,顺序不改变
程序代码如下:
/* 2017年3月9日11:26:53 功能:将数字字符后面插入*,顺序不改变 */ #include"stdio.h" int main() { char a[100]; char b[100]; char c[100]; char *pa = a; char *ppa = a; char *pb = b; char *ppb = b; //该定义的目的是为了将指针移动到串首 char *pc = c; printf("please input a string : "); gets(a); while(*pa) //此循环部分是统计有多少个数字个数,同理就是插入几个* { if((*pa) >= '1' && (*pa) <= '9') //将*放入到新的内存空间 { (*pb) = '*'; pb++; } pa++; //不论此时循环的字符是何种形式,指针都需要将指针后移 } *pb = ' '; while(*ppa) { if((*ppa) >= '1' && (*ppa) <= '9') { (*pc) = (*ppa); pc++; //pc++;为了找到下次需要存储的位置 (*pc) = (*ppb); ppb++; //ppb++;为了找到下次需要插入的数据 pc++; } else { (*pc) = (*ppa); pc++; } ppa++; //不论何种形式,进行操作的字符串都要找到下一次要判断的字符,故进行ppa++操作 } *pc = ' '; puts(c); return 0; } /* 总结: 在VC++6.0中显示的结果: ———————————————————————— please input a string : asdfdgfg32536xvgvdg# asdfdgfg3*2*5*3*6*xvgvdg# ———————————————————————— */