完整复制字符串
问题及代码
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:辛彬
* 完成日期:2014年 11 月 26 日
* 版 本 号:v1.0
*
* 问题描述: 完整复制字符串。。
* 输入描述:没有输入。
* 程序输出:完整字符串;
*/
#include<iostream>
using namespace std;
int main()
{
char str1[50]="I am a happy boy\'s daddy.",str2[50];
int i=0,j=0;
while(str1[i]!='\0')
{
str2[j]=str1[i];
j++;
i++;
}
str2[j]='\0';//切记!!
cout<<"整理后的字符串"<<str2<<endl;
return 0;
}
运行结果:
去除str1中的空格,仍保存在str1中
问题及代码
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:辛彬
* 完成日期:2014年 11 月 26 日
* 版 本 号:v1.0
*
* 问题描述: 去除str1中的空格,仍保存在str1中。
* 输入描述:没有输入。
* 程序输出:除str1中的空格;
*/
#include<iostream>
using namespace std;
int main()
{
char str1[50]="I am a happy boy\'s daddy.";
int i=0,j=0;
while(str1[i]!='\0')
{
if(str1[i]!=' ')
{
str1[j]=str1[i];
i++;
j++;
}
else
i++;
}
str1[j]='\0';
cout<<"整理后的字符串"<<str1<<endl;
return 0;
}
运行结果:
学习感悟:注意结束符的位置,第二个项目要尽量用一个数组,不然会出错