请问这个程序什么错误???

来源:百度知道 编辑:UC知道 时间:2024/06/07 07:15:10
#include<stdio.h>
# include <iostream.h>
#define N 80
void del_char(char *p,char x)
{
char a[N];
p=a;
while (*p != '\0')
{
if (*p != x)
cout<<*p;
p++;
}
}

void main( )
{
char c[N],*pt=c,x;
cout<<"enter a string:";
cin>>pt;
cout<<"enter the char deleted:"<<endl;
cin>>x;
del_char (pt,x);
cout<<"The new string is :"<<c<<endl;
}
题目要求是:设计一个程序,在main( )中输入一个字符串,然后再输入一个字符,调用一个函数void del_char(char *p,char x),删除在字符串中的这个字符(要求实参、形参都用指针表示)。我的程序为什么不对呢?
请问怎么改呢? 就是如何才能做到把x删除呢?

#include<stdio.h>
# include <iostream.h>
#define N 80
void del_char(char *p,char x)
{
//char a[N];不要这两行。没用的。
//p=a;
while (*p != '\0')
{
if (*p != x)
cout<<*p; /*这里只是不输出x,并没有从内存中删除,所以在主函数中并没有你想得的结果。*/
p++;
}
}

void main( )
{
char c[N],*pt=c,x;
cout<<"enter a string:";
cin>>pt;
cout<<"enter the char deleted:"<<endl;
cin>>x;
del_char (pt,x);
cout<<"The new string is :"<<c<<endl;
}

//正确的答案,仅供参考:
#include<stdio.h>
# include <iostream.h>
//#define N 80 //你定义的东西太多了。
char *del_char(char *p,char x) //定义指针函数
{
char *a,*ppt;
a=p; //初始化指针
ppt=a; //初始化指针
while (*p != '\0')
{
if (*p != x)
*a++=*p; //用一个数组保存输出的字符
p++;
}
*a=&#