2014秋C++第13周项目5参考-字符串操作
来源:互联网 发布:淘宝链接 编辑:程序博客网 时间:2024/05/03 12:57
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。
统计每一个数字字符出现的次数;
(2)阅读下面的程序,完成类似字符串复制的功能
还可以写作:
还可以写作:
怕被绕更迷糊了的,不要看下面的程序
【项目5-字符串操作】
(1)阅读下面的程序,完成类似的字符统计功能
#include<iostream>#include<cstdio> using namespace std;int main(){char str[50];int i=0,n=0;cout<<"输入字符串:";gets(str);while(str[i]!='\0'){ if(str[i]>='0'&&str[i]<='9') n++;i++;}cout<<"其中的数字个数是: "<<n<<endl;return 0;}请分别编制程序,完成下面的处理(选2个):
统计字母'A'出现的次数;
#include<iostream>#include<cstdio>using namespace std;int main(){char str[50];int i=0,n=0;cout<<"输入字符串:";gets(str);while(str[i]!='\0'){if(str[i]=='A') n++;i++;}cout<<"其中字母A个数是: "<<n<<endl;return 0;}
统计字符串中(大/小写)字母个数;
#include<iostream>#include<cstdio>using namespace std;int main(){ char str[50]; int i=0,up=0,low=0; cout<<"输入字符串:"; gets(str); while(str[i]!='\0') { if(str[i]>='A'&&str[i]<='Z') up++; else if(str[i]>='a'&&str[i]<='z') low++; i++; } cout<<"其中大写字母个数是: "<<up<<endl; cout<<"其中小写字母个数是: "<<low<<endl; return 0;}
统计每一个数字字符出现的次数;
提示:可以定义一个数组int a[10],保存’0’-’9’在字符串中出现的次数(例a[0]表示字符’0’的个数)
#include<iostream>using namespace std;int main(){ char str[50]= {"Tel:18363803401, QQ:90878965"}; int i,a[10]= {0}; //a[0]代表'0'出现的个数……,初值全为0 for(i=0; str[i]!='\0'; ++i) //换种循环,这不是关键 { if(str[i]>='0'&&str[i]<='9') a[str[i]-'0']++; //若str[i]为'0',a[0]将增加;也可以写作a[str[i]-48]++ } cout<<"\""<<str<<"\" 中各数字出现的次数是: "<<endl; for(i=0; i<10; ++i) cout<<"\'"<<i<<"\'---"<<a[i]<<endl; return 0;}
统计每一个字母出现的个数
#include<iostream>using namespace std;int main(){ char str[100]= {"Checking for existence: D:\\C++\\codeBlock\\example\\bin\\Debug\\example.exe"}; int i,a[26]= {0}; //a[0]代表'a'出现的个数……,初值全为0 for(i=0; str[i]!='\0'; ++i) //换种循环,这不是关键 { if(str[i]>='a'&&str[i]<='z') a[str[i]-'a']++; //若str[i]为'a',a[0]将增加;也可以写作a[str[i]-97]++ } cout<<"\""<<str<<"\" 中各小写字母出现的次数是: "<<endl; for(i=0; i<26; ++i) cout<<"\'"<<char(i+97)<<"\'---"<<a[i]<<endl; return 0;}
(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;}请分别编制程序,完成下面的处理(选2个):
完整复制字符串;
#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;}
将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; //下面复制str2while(str2[j]!='\0'){str3[i++]=str2[j++];}str3[i]='\0'; //一定要保证str3中有'\0',否则"烫烫烫烫"cout<<"处理后的字符串是: "<<str3<<endl;return 0;}
去除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]不是空格,将复制elsej++; //str[j]为空格时,没有进行复制//j++而没有i++,所以只要有空格,j的值一定会大于i的//不管是否有空格,j一直勇往直前,i则遇空格就休息}str[i]='\0'; //str[i]处是复制结束后的位置cout<<"处理后的字符串是: "<<str<<endl;return 0;}
看下面的截图,理解程序。如果困难,自己单步执行观察一下。
#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; //下面复制str2while(str2[j]!='\0'){str1[i++]=str2[j++];}str1[i]='\0'; //一定要保证str3中有'\0',否则"烫烫烫烫"cout<<"处理后的字符串是: "<<str1<<endl;return 0;}
=================== 迂者 贺利坚 CSDN博客专栏=================|== IT学子成长指导专栏 专栏文章的分类目录(不定期更新) ==||== C++ 课堂在线专栏 贺利坚课程教学链接(分课程年级) ==||== 我写的书——《逆袭大学——传给IT学子的正能量》 ==|===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =====
0 0
- 2014秋C++第13周项目5参考-字符串操作
- 第13周项目5--字符串操作
- 第13周项目5-字符串操作
- 2014秋C++第13周项目6参考-体验文件操作
- 第13周项目5-字符串操作--字符串复制
- 第13周项目 5.1 字符串操作
- 2014秋C++第16周 项目2参考 用指针玩字符串
- 2014秋C++第16周 项目2参考 用指针玩字符串
- 第13周项目5(1-1)-字符串操作
- 第13周项目5(1-2)-字符串操作
- 第13周项目5(1-3)-字符串操作
- 第13周项目5(1-4)-字符串操作
- 第13周项目5(2-1)-字符串操作
- 第13周项目5(2-2)-字符串操作
- 第13周项目5(2-3)-字符串操作
- 第13周项目5(2-4)-字符串操作
- 第13周项目5-字符串操作(1.2)
- 第13周项目5-字符串操作(2.1)
- 2014秋C++第13周项目2参考-成绩处理
- 2014秋C++第13周项目3参考-成绩处理函数版
- 深入认识Tigase XMPP Server(下)
- Struts2之模型驱动及分层架构初识
- 2014秋C++第13周项目4参考-数组的排序
- 2014秋C++第13周项目5参考-字符串操作
- JTree用法
- 2014秋C++第13周项目6参考-体验文件操作
- php引用(&)详解及注意事项
- Linux下tar.xz结尾的文件的解压方法
- Qt 开发之三 :stylesheet
- Atitit. Java script 多重多重catch语句的实现and Javascript js 异常机制
- libosmscout读取地图的C++库
- Atitit. Dwr 抛出异常error解决方案