- 12
- 0
- 约5.45千字
- 约 19页
- 2017-07-02 发布于湖北
- 举报
C程序设计的string类概要1
5.6.2 标准的C++string类 (4) string类的输入输出。输出与C风格字符串同样方便,使用插入运算符和cout。输入如用提取运算符,代码读取的是以空白字符结束的字符串,输入完整的字符串可用非成员函数getline,注意格式: getline(cin,str); //串以’\n’结束 getline(cin,str,ch); //串以ch结束 (5) string类有一些常用的成员函数可进行字符串处理: str.substr(pos,length1); //返回对象的一个子串,从pos位置起,长length1个字符 str.empty( ); //查是否空串 str.insert(pos,str2); //将str2插入str的pos位置处 str.remove(pos,length1); //在str位置pos处起,删除长度为length1的字串 str.find(str1); //返回str1首次在str中出现时的索引 str.find(str1,pos); //返回从pos处起str1首次在str中出现时的索引 str.length(str); //返回串长度 (6) C字符串到string类对象是由构造函数隐式自动进行,而string类对象到C字符串的转换必须执行显示的类型转换,应调用成员函数 str.c_str( ); //将string类转换为C风格字符串,返回char* 回文是指顺读和反读都一样的串,这里不分大小写,并滤去所有非字母字符,如: Madam,I’m Adam. Golf,No Sir, prefer prison flog! 都是回文。 bool is_pal(const string s){ //判断是否回文 string punct(,;:.?\ ); //包括空格符 string str(make_lower(s)); str=remove_punct(str,punct); //滤去所有非字母字符 return str==reverse(str);} string make_lower(const string s){ //所有大写改为小写 string temp(s); int i,s_length=s.length(); for(i=0;is_length;i++) temp[i]=tolower(s[i]); return temp;} void swap(char ch1,char ch2){ //交换两个字符 char temp=ch1; ch1=ch2; ch2=temp;} string reverse(const string s){ //返回反转的字符串 int start=0,end=s.length(); string temp(s); while(startend){ end--;swap(temp[start],temp[end]);start++; } return temp;} string remove_punct(const string s,const string punct){ string no_punct; //放置处理后的字符串 int i,s_length=s.length(),p_length=punct.length(); for(i=0;is_length;i++){ string a_ch=s.substr(i,1); //单字符string int location=punct.find(a_ch,0);//从头查找a_ch在punct中出现的位置 if(location0||location=p_length) no_punct=no_punct+a_ch; //punct中无a_ch,a_ch拷入新串 } return no_punct; } //将第一个字符串中所包含的与第二个字符串中相同的字符删去 int main( ) { string str; cout请输入需判断是否为回文的字符串,以回车结束。\n; getline(cin,str); if(is_pal(str)) coutstr是回文。\n; else coutstr不是回文。\n; return 0; } * * 字符数组即数组中的每一个元素是字符,在C++语言中字符型数组的应用很多: (1)
您可能关注的文档
- BusinessObjects XI 管理(公司内部资料)概要1.ppt
- Business_appointment_商务会谈概要1.ppt
- But what`s a dictionary for? 课概要1.pptx
- Butterknife用法概要1.doc
- But you didn’t概要1.pptx
- Business Etiquette(商务礼仪2014)概要1.pptx
- BWDSP汇编入门概要1.pptx
- buzz marketing概要1.ppt
- byron she walks in beauty 美与媚概要1.ppt
- Business travel概要1.pptx
- 2025年全国演出经纪人员资格认定考试试卷带答案(研优卷).docx
- 2025年全国演出经纪人员资格认定考试试卷完整版.docx
- 2025年全国演出经纪人员资格认定考试试题库及完整答案.docx
- 2025年全国演出经纪人员资格认定考试试卷完美版.docx
- 2025年全国演出经纪人员资格认定考试试卷含答案(实用).docx
- 2025年全国演出经纪人员资格认定考试试卷及答案(各地真题).docx
- 2025年下半年内江市部分事业单位公开考试招聘工作人员(240人)备考题库附答案.docx
- 2025年全国演出经纪人员资格认定考试试卷及答案1套.docx
- 2025年下半年四川成都市郫都区面向社会引进公共类事业单位人员2人备考题库最新.docx
- 2025年下半年内江市部分事业单位公开考试招聘工作人员(240人)备考题库附答案.docx
最近下载
- 西方经济学-马工重点教材-第1章.ppt VIP
- nex-6使用说明书手册.pdf
- 西方经济学-马工程重点教材-第3章.ppt VIP
- 西方经济学-马工重点教材-第2章.ppt VIP
- 2025年安徽省公务员考试法律专业试卷(真题及答案) .pdf VIP
- 2025台州三门县国有企业公开招聘工作人员33人笔试参考试题附答案解析.docx VIP
- 电力系统优化与调度软件:AIMMS二次开发_(3).AIMMS中的数学优化理论.docx VIP
- 《中国黄金集团内蒙古矿业有限公司较大坠落事故调查报告》公布.docx VIP
- 市政工程施工组织设计(范本).doc VIP
- 能源优化建模:AIMMS二次开发_(2).AIMMS基础与高级编程.docx VIP
原创力文档

文档评论(0)