/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:呼亚萍
* 完成日期:2014年 12 月 11日
* 版 本 号:v1.0
*
* 问题描述:去除字符串str1中的特定字符(空格),结果仍保存在原字符串中
* 输入描述:相应的程序
* 程序输出:判断结果
*/
#include <iostream>
#include <cstdio>
using namespace std;
char *astrcat(char str1[]);
int main()
{
char s1[50]="Hello world. ";
char s2[50]="Good morning. ";
char s3[50]="vegetable bird! ";
astrcat(s1);
astrcat(s2);
astrcat(s3);
cout<<astrcat(s1)<<endl;
cout<<astrcat(s2)<<endl;
cout<<astrcat(s3)<<endl;//返回值为char*型,可以直接显示
return 0;
}
char *astrcat(char str1[])
{
int i=0,j=0;
while(str1[i]!='\0')
{
if(str1[i]!=' ')
{
str1[j++]=str1[i++];
}
else
i++;
}
str1[j]='\0';//切记!!
return str1;
}
运算结果:
知识点总结:
while语句的使用,函数的调用,当遇到空格时,不输出,继续加1,再输出。
学习心得:
要习惯使用单步执行,找出问题,继续努力,加油!