C++ 复制字符串/字符数组
来源:互联网 发布:java高并发架构 编辑:程序博客网 时间:2024/06/06 02:17
题目
【项目1-小心地放开玩字符串/字符数组】
(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;
- for(i=0;str1[i]!='\0';i++)
- {
- if(str1[i]!=' ')
- {
- str2[j]=str1[i];
- j++;
- }
- }
- str2[j]='\0';//切记!!
- cout<<"整理后的字符串"<<str2<<endl;
- return 0;
- }
- 复制字符串str1,保存到str2中;
- 在字符串str2后面增加符号"//comment:"
- 将str1和str2接起来存放到str3中;
- 将str1中的空格去除,仍保存在str1中;
- 将str1和str2接起来,仍存放在str1中;
- 去除str1中的多余空格(即两个单词中间仅保留一个空格)。
- 在字符串前加5个符号"note:"
二、参考解答
1、复制字符串str1,保存到str2中
- #include<iostream>
- using namespace std;
- int main()
- { char str1[50]="I am a happy boy\'s daddy.",str2[50];
- int i,j;
- for(i=0,j=0;str1[i]!='\0';i++)
- {
- str2[j]=str1[i];
- ++j;
- }
- str2[j]='\0';//切记!!
- cout<<"整理后的字符串"<<str2<<endl;
- return 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;
- for(i=0;str1[i]!='\0';)
- {
- str2[j++]=str1[i++];//注意下标处的写法
- }
- str2[j]='\0';//切记!!
- cout<<"整理后的字符串"<<str2<<endl;
- return 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((str2[j++]=str1[i++])!='\0');//如此表达式!循环体为空
- cout<<"整理后的字符串"<<str2<<endl;
- return 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(str2[j++]=str1[i++]); //其实和上面的程序等价
- cout<<"整理后的字符串"<<str2<<endl;
- return 0;
- }
2、在字符串str2后面增加符号"//comment:"
课堂上我提示找到str2的’\0’处,然后逐个赋值,太笨了。现在决定将要增加的字符串在程序中用一个字符串给出来,这是更能通用的办法。
- #include<iostream>
- using namespace std;
- int main()
- {
- char str2[50];
- char str[20]={"//注释: 定义变量"};
- int i=0,j=0;
- cout<<"输入字符串:";
- gets(str2);
- while(str2[i]!='\0')
- {
- i++;
- }
- while(str[j]!='\0')
- {
- str2[i++]=str[j++];
- }
- str2[i]='\0';
- cout<<"--------------------------"<<endl;
- cout<<"处理后的字符串是: "<<str2<<endl;
- return 0;
- }
3、将str1和str2接起来存放到str3中;
- #include<iostream>
- using namespace std;
- int main()
- {
- char str1[20]={"int i,a[100];"};
- char str2[20]={"//注释: 定义变量"};
- char str3[40];
- int i=0,j=0;
- while(str1[j]!='\0') //复制str1
- {
- str3[i++]=str1[j++];
- }
- j=0; //下面复制str2
- while(str2[j]!='\0')
- {
- str3[i++]=str2[j++];
- }
- str3[i]='\0'; //一定要保证str3中有'\0',否则"烫烫烫烫"
- cout<<"处理后的字符串是: "<<str3<<endl;
- return 0;
- }
4、将str1和str2接起来,仍存放在str1中
- #include<iostream>
- using namespace std;
- int main()
- {
- char str1[40]={"I am a boy."};
- char str2[20]={"//注释: 废话一句"};
- int i=0,j=0;
- while(str1[i]!='\0') //找到str1的结束
- {
- i++;
- }
- j=0; //下面复制str2
- while(str2[j]!='\0')
- {
- str1[i++]=str2[j++];
- }
- str1[i]='\0'; //一定要保证str3中有'\0',否则"烫烫烫烫"
- cout<<"处理后的字符串是: "<<str1<<endl;
- return 0;
- }
5、将str1中的空格去除,仍保存在str1中
策略不是去除空格,而是复制非空格字符,不复制空格,这样,空格挂了。
在现实中,迟到的员工罚款,是一种办法;还有一种做法,奖励按时到的员工,迟到的没有奖。都是奖勤罚懒。
- #include<iostream>
- using namespace std;
- int main()
- {
- char str[40]={"I am a boy."};
- int i=0,j=0;
- while(str[j]!='\0') //找到str的结束
- {
- if(str[j]!=' ')
- str[i++]=str[j++]; //如果str[i]不是空格,将复制
- else
- j++; //str[j]为空格时,没有进行复制
- //j++而没有i++,所以只要有空格,j的值一定会大于i的
- //不管是否有空格,j一直勇往直前,i则遇空格就休息
- }
- str[i]='\0'; //str[i]处是复制结束后的位置
- cout<<"处理后的字符串是: "<<str<<endl;
- return 0;
- }
看下面的截图,理解程序。如果困难,自己单步执行观察一下。
6、去除str1中的多余空格(即两个单词中间仅保留一个空格)。
- #include <iostream>
- using namespace std;
- int main()
- {
- char str[]="Only one space is allowed between the two words.";
- cout<<"原始难看的句子:"<<str<<endl;
- int i=0,j=0;
- bool notSpace;
- while(str[j]==' ')//忽略开始的若干个空格
- j++;
- notSpace=true;
- while(str[j]!='\0')
- {
- if (str[j]!=' ')
- {
- notSpace=true;
- str[i++]=str[j++]; // 不是空格,复制
- }
- else if (notSpace) //是空格,但目前是第一个(因为之前notSpace=true;的条件是遇非空格)
- {
- notSpace=false; //第一个空格仍然要复制
- str[i++]=str[j++];
- }
- else //如遇第二个或更后的空格,正是由于遇到第一个空格并复制后,notSpace=false;的原因,会走到这儿
- {
- j++; //不复制
- }
- }
- str[i]='\0';
- cout<<"整理后的句子是:"<<str<<endl;
- return 0;
- }
7、在字符串前加5个符号"note:"
- #include<iostream>
- using namespace std;
- int main()
- {
- char str1[40]={"I am a boy."};
- char str2[20]={"note:"};
- int i=0,len2=0;
- //下面将把str2插入到str1的开头部分,并且保存在str1中
- //先求出str2的“长度”
- while(str2[len2]!='\0')
- {
- len2++;
- }
- //str2中有len2个字符
- //找到str1中结尾的位置
- i=0;
- while(str1[i]!='\0')
- {
- i++;
- }
- //由后往前,整体往后“搬迁”len2个位置
- while(i>=0)
- {
- str1[i+len2]=str1[i];
- i--;
- }
- //空出的前len2位置复制为str2
- i=len2-1;
- while(i>=0)
- {
- str1[i]=str2[i];
- i--;
- }
- cout<<"处理后的字符串是: "<<str1<<endl;
- return 0;
- }
0 0
- C++ 复制字符串/字符数组
- 字符串,字符数组(c/c++)
- C语言字符串、字符数组
- C 字符数组和字符串
- C字符数组与字符串
- 字符指针指向的字符串复制到字符数组里
- 逐个字符复制一个C-串到字符数组中
- c语言中的字符数组与字符串
- c语言中的字符数组与字符串
- c语言中字符串与字符数组
- C Java 字符串和字符数组区别
- C 语言里的字符串---字符数组
- c语言中的字符数组与字符串
- c语言 字符数组 与 字符串
- C语言中的字符数组与字符串
- C语言—字符串(字符数组)
- c语言中的字符数组与字符串
- C语言 字符数组和字符串
- READING NOTE: FastMask: Segment Multi-scale Object Candidates in One Shot
- 新水手上船
- Java垃圾回收
- RNN时间序列预测(1)-Tensorflow入门,MNIST学习
- LeetCode 141 Linked List Cycle(链表判环)
- C++ 复制字符串/字符数组
- 巧用模板,不仅能提升AS开发效率,还能装逼
- Linux Kernel 学习笔记4:自动创建设备节点
- MongoDB的安装和配置
- 系统延迟及定时机制
- C#中using用法
- fl2440添加USB驱动,并添加ntfs的写入
- 机器学习笔记(三) 朴素贝叶斯分类
- 拍卖(京东2017实习生招聘真题)