问题描述:
(2)阅读下面的程序,完成类似字符串复制的功能
#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')
{
if(str1[i]!=' ')
{
str2[j]=str1[i];
j++;
}
i++;
}
str2[j]='\0';//切记!!
cout<<"整理后的字符串"<<str2<<endl;
return 0;
}
请分别编制程序,完成下面的处理
(3)去除 str1 中的空格,仍保存在 str1 中;
代码:
#include<iostream>
using namespace std;
int main()
{
char str1[1000]="One thorn of experience is worth a whole wilderness of warning. ";
int i=0,j=0;
while(str1[j]!='\0')
{
if(str1[i]!=' ')
str1[j++]=str1[i++];
else
i++;
}
str1[j]='\0';
cout<<"整理后的字符串"<<str1<<endl;
return 0;
}
运行结果: