提取字符串,并对字符串按字典序排序
来源:互联网 发布:淘宝付费软件的 编辑:程序博客网 时间:2024/04/28 00:44
从标准输入读取任意多个字符串,字符串用#分割。最后输入一个固定的标识输入结束的字符串,如“;\*.”,对输入的所有字符串按字典序排,并输出到一个文件中。同时统计字符串的个数、最长字符串和最短字符串。
使用strtok提取字符串
//// main.cpp// str//// Created by Bryan on 14-6-28.// Copyright (c) 2014年 Bryan. All rights reserved.//#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;struct myStr{ string s; int len;};bool SortByAlpha(myStr &a, myStr &b){ return a.s<b.s;}bool SortByStrLen(myStr &a, myStr &b){ return a.len<b.len;}vector<myStr> ExtractString(const string &str){ vector<myStr> ret; char * cstr = new char[str.length() + 1]; strcpy(cstr,str.c_str()); char * p = strtok(cstr, "#;\\*."); while(p) { string tmpS(p); int len = tmpS.length(); myStr tmp; tmp.s = tmpS; tmp.len = len; ret.push_back(tmp); p = strtok(NULL, "#;\\*."); } return ret; }int main(int argc, const char * argv[]){ string str; cout<<"Please enter a string:"; cin>>str; vector<myStr> ret = ExtractString(str); cout<<"Before Sort:"<<endl; for(vector<myStr>::iterator it = ret.begin(); it != ret.end();it++) { cout<<it->s<<endl; } cout<<endl; cout<<"After Sort By alphabet:"<<endl; sort(ret.begin(),ret.end(),SortByAlpha); for(vector<myStr>::iterator it = ret.begin(); it != ret.end();it++) { cout<<it->s<<endl; } cout<<endl; cout<<"After Sort By strLength:"<<endl; sort(ret.begin(),ret.end(),SortByStrLen); for(vector<myStr>::iterator it = ret.begin(); it != ret.end();it++) { cout<<it->s<<" length: "<<it->len<<endl; } return 0;}
之前自己写的方法,跟库函数相比麻烦太多
#include <iostream>#include <fstream>#include <vector>const int N = 1000;using namespace std;struct struct_str{char* ch;int length;};int StrDeal(char* str,std::vector<struct_str>& strvector){int strEnd = 0;int i = 0;while(str[i] != '\0'){if(str[i] == ';' && str[i+1] == '\\' && str[i+2] == '*' && str[i+3] == '.')break;i++;}strEnd = i-1;int localStrStart = 0;int localStrEnd = 0;while(str[localStrStart] == '#' && localStrStart<= strEnd)localStrStart++;localStrEnd = localStrStart;while(localStrEnd <= strEnd){while(str[localStrEnd] != '#' && localStrEnd <= strEnd)localStrEnd++;struct_str tmpStrStruc;tmpStrStruc.ch = new char[N];int tmpStrLen = localStrEnd - localStrStart;for(int i = 0;i < tmpStrLen;i++)tmpStrStruc.ch[i] = str[localStrStart + i];tmpStrStruc.ch[tmpStrLen] = '\0';tmpStrStruc.length = tmpStrLen;strvector.push_back(tmpStrStruc);localStrStart = localStrEnd + 1;localStrEnd = localStrStart;while(str[localStrStart] == '#' && localStrStart<= strEnd)localStrStart++;localStrEnd = localStrStart;}return strvector.size();}void StrCmp(std::vector<struct_str>& strvector){int countN = strvector.size();int *a = new int[countN];for(int i = 0;i<countN;i++)a[i] = i;/*for(int j = 1;j < countN;j++){int k = j - 1;int tmp = a[j];while(k >= 0 && strcmp(strvector[a[k]].ch,strvector[a[j]].ch) > 0){a[k+1] = a[k];k = k-1;}a[k+1] = tmp;}*/char myStr[N];int tmp;for(int i = 0;i<countN;i++)for(int j = i;j<countN;j++){if(strcmp(strvector[i].ch,strvector[j].ch)>0){strcpy(myStr,strvector[i].ch);strcpy(strvector[i].ch,strvector[j].ch);strcpy(strvector[j].ch,myStr);tmp = strvector[i].length;strvector[i].length = strvector[j].length;strvector[j].length = tmp;}}for (int i = 0;i<countN;i++){cout<<strvector[i].ch<<endl;}int maxLen = strvector[0].length;int minLen = strvector[0].length;int maxIndex = 0;int minIndex = 0;for(int i = 0;i< countN;i++){if(strvector[i].length > maxLen){maxIndex = i;maxLen = strvector[i].length;}if(strvector[i].length < minLen){minIndex = i;minLen = strvector[i].length;}}ofstream out("result.txt",ios::out);if(!out){cerr<<"Open file error!";exit(1);}for(int i=0;i<countN;i++)out<<strvector[i].ch<<endl;out<<"最短的字符是:"<<strvector[minIndex].ch<<endl;out<<"最长的字符是:"<<strvector[maxIndex].ch<<endl;}int main(){ std::vector<struct_str> strVector;char *str = new char[N];cout<<"Input a string:"<<endl;//cin>>str; //遇到空格就会终止读入gets_s(str,N); //或者用gets(str);StrDeal(str,strVector);StrCmp(strVector);getchar();//int i,j,n=N;//char str[N][80]={"English","Math","Computer","Physics","Database"};//char mystring[80];////字符串排序//for(i=0;i<N;i++)//{//for(j=i;j<N;j++)//{//if(strcmp(str[i],str[j])>0)//{//strcpy(mystring,str[i]);//strcpy(str[i],str[j]);//strcpy(str[j],mystring);//}//}//}////输出排序后的字符串//cout<<"sorted strings:"<<endl;//for(i=0;i<n;i++)//cout<<str[i]<<endl;//getchar();//return 0;}////####123#4567#hnf,v.;d;\*.//bdc#abc#mln#hjk#abcd;\*.///mln#hjk#bgfl#qwert#abcgt#dkfmg.#lfkfj#fdf;\*.//English#Math#Computer#Physics#Database;\*.
0 0
- 提取字符串,并对字符串按字典序排序
- 提取字符串,并对字符串按字典序排序
- 字符串按字典排序
- 字符串按字典排序
- 提取字符串中的元音字母并排序
- 个人模板 字符串按字典序排序
- 字符串按照字典序排序
- 字符串排序(字典序)
- 按字典序列排序字符串
- 字符串字典排序
- 字符串排序-字典顺序
- 将n个字符串中的数字提取并排序
- 用系统方法提取字符串首字母并进行排序
- C语言读取字符串,并对字符串排序
- Android 获取字符串首字母,并对字符串列表进行排序。
- lexicographical_compare()按字典序比较函数用法示例(字符串排序)
- c读入文件,多个字符串,按字典序排序
- 实现几个字符串的字典序排序
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- java线程池使用场景和使用方法较详细文摘
- 打开图库
- <转>简单的java缓存实现
- STL常见用法整理_list
- 提取字符串,并对字符串按字典序排序
- 最终要到哪里去
- 温故而知新-面向对象设计原则
- windows上远程运行MapReduce任务遇到的问题
- hdu3078 建层次树+在线LCA算法+排序
- 针对TCP协议的攻击与检测、预防方法
- Tomcat下配置HTTPS
- C 语言的可变参数表函数的设计
- 我们离人工智能有多远?