<pre class="cpp" name="code">/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘畅
* 完成日期:2014 年 12 月 11 日
* 版 本 号:v1.0
*
* 问题描述:实现char *pdelchar(char *str, const char c)函数;
* 输入描述:无需输入;
* 程序输出:输出要求输出的。
(1)用数组名做行参;
#include <iostream>
using namespace std;
char *adelchar(char str[],const char c);
int main()
{
char s1[50]="Hello world. ";
char s2[50]="Good morning. ";
char s3[50]="vagetable bird! ";
cout<<adelchar(s1,' ')<<endl;
cout<<adelchar(s2,' ')<<endl;
cout<<adelchar(s3,' ')<<endl;
return 0;
}
char *adelchar(char str[],const char c)
{
int i=0,j=0;
while (str[i]!='\0')
{
if (str[i]!=c)
str[j++]=str[i++];
else
i++;
}
str[j]='\0';
return str;
}
(2)用指针作行参;
#include <iostream>
using namespace std;
char *adelchar(char *str,const char c);
int main()
{
char s1[50]="Hello world. ";
char s2[50]="Good morning. ";
char s3[50]="vagetable bird! ";
cout<<adelchar(s1,' ')<<endl;
cout<<adelchar(s2,' ')<<endl;
cout<<adelchar(s3,' ')<<endl;
return 0;
}
char *adelchar(char *str,const char c)
{
int i=0,j=0;
while (*(str+i)!='\0')
{
if (*(str+i)!=c)
{
*(str+j)=*(str+i);
i++;
j++;
}
else
i++;
}
*(str+j)='\0';
return str;
}
运行结果:
学习心得:
沉下心来做,总算是理解这些项目是要做什么了,虽然对指针的运用还不够娴熟,也不大理解指针在这些地方的应用有什么意义,不过还是为又学会了一种对指针的运用开心。