编写函数 char *insert(char *p),其功能是在p指向的字符串中所有数字字符子串前插入一个符号‘-’。函数返回p字符串的首地址。
执行insert方法后,输出时,漏了最后一个字符
#include
#include
#include
#include
char *insert(char *p){
char *q=p,temp[20];
while(*p){
while(*p&&isalpha(*p)) p++;
if(*p){
strcpy(temp,p);
*p++='-';
strcpy(p,temp);
}
while(*p&&isdigit(*p)) p++;
}
return q;
}
int main(){
char p[]="AB1CD12EF123GH",*q;
q=insert(p);
printf("%s",q);
return 0;
}
char p[]="AB1CD12EF123GH" 没有指定数组大小,往数组中插入字符,会导致溢出越界。给数组一个比字符串大的长度。char p[30]="AB1CD12EF123GH"