急救

fscanf 2003-09-06 05:19:32
#include <iostream.h>

void insertch(char *s,char ch,int pos);
void deletech(char *s,int pos);

void insertch(char *s,char ch,int pos)
{
int i;

for(i=strlen(s)+1;i>pos;i--)
s[i]=s[i-1];
s[i]=ch;
}

void deletech(char *s,int pos)
{
int i;

for(i=pos;i<strlen(s);i++)
s[i]=s[i+1];
}

main()
{
char string[]={"the is C++ programming"};
char ch;

cout<<"ch:";
cin>>ch;
insertch(string,ch,3);
cout<<"string:"<<string<<endl;

deletech(string,6);
cout<<"string:"<<string<<endl;
}

程序通过编译,运行得不到正确的输出。
...全文
23 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fscanf 2003-09-07
  • 打赏
  • 举报
回复
我在TC下结果也正确,但是dev-c++就出错了
nirvana_ya 2003-09-06
  • 打赏
  • 举报
回复
对strlen()函数进行库的包含,我用<string.h>
结果正确.
但希望char string[]能比所要存的字符数多,最好定义一个最大的范围.
这是可能出错的地方.因为insert操作可能取道string以外的范围
ehhl 2003-09-06
  • 打赏
  • 举报
回复
不错,在VC6下可以
同意楼上
紫郢剑侠 2003-09-06
  • 打赏
  • 举报
回复
同意楼上兄弟的观点.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧