获取字符串中的数字串
来源:互联网 发布:科比2003年总决赛数据 编辑:程序博客网 时间:2024/06/04 21:40
#include <cstring>#include <cstdlib>#include <iostream>#include <vector>using namespace std;// 获取字符串中的数字串void pickNum(const char* str, vector<char*>& numVec){// 验证参数有效性if(str == NULL || strlen(str) == 0){cerr << "invalid parameter";return;}const char* start = str;while(*start != '\0'){// 定位到数字字符串的头while(*start != '\0' && (*start > '9' || *start < '0')){++start;}const char* end = start;// 定位到数字字符串尾下一个字符while(*end != '\0' && *end >= '0' && *end <= '9'){++end;}if(*start != '\0'){int len = end - start;char* numStr = new char[len + 1];memcpy(numStr, start, len);numStr[len] = '\0';numVec.push_back(numStr);}start = end;}}int main(int argc, char* argv[]){// 测试用例vector<const char*> strVec;strVec.push_back(NULL);strVec.push_back("");strVec.push_back("abc");strVec.push_back("a1b2c");strVec.push_back("a12b2c13");strVec.push_back("0a1b2c3");strVec.push_back("01234");for(int strIdx = 0; strIdx < strVec.size(); ++strIdx){cout << strIdx << " : ";if(strVec[strIdx] != NULL){cout << strVec[strIdx];}cout << " : ";vector<char*> numVec;pickNum(strVec[strIdx], numVec);for(int numIdx = 0; numIdx < numVec.size(); ++numIdx){cout << numVec[numIdx] << ' ';}cout << endl;}return 0;}
运行结果:
0 : : invalid parameter
1 : : invalid parameter
2 : abc :
3 : a1b2c : 1 2
4 : a12b2c13 : 12 2 13
5 : 0a1b2c3 : 0 1 2 3
6 : 01234 : 01234
0 0
- 获取字符串中的数字串
- javascript 获取字符串中的最后一个数字串
- 25.找出字符串中的最长数字串
- 找出字符串中的最长连续数字串
- java求得字符串中的最长数字串
- java提取字符串中的数字串
- 找出字符串中的最长数字串
- vbs 获取字符串字串
- 字符串中数字串相加;字符串中最大数字串;
- 计算字符串中的单词数. 单词:由空格分开的连续字母数字串。
- 计算字符串中的单词数(单词:由空格分开的连续字母数字串)
- 读入一个字符串str,输出字符串str中的连续最长的数字串
- 读入一个字符串str,输出字符串str中的连续最长的数字串
- 字符串中的最长重复字串
- 计算一个字符串中的字串
- 微软100题(25)找出字符串中的最长数字串
- 找出字符串中最长的数字串
- 字符串中最长的数字串
- Linux内核Ramdisk(initrd)机制
- 黑马程序员————Java基础之面向对象(二)
- Seam 对话以及工作区管理
- UserCF与ItemCF杂想之余弦相似度
- Ubuntu12.04下安装Qt4和创建第一个Qt程序
- 获取字符串中的数字串
- iOS中对UITableView中的cell设置自动适应行高
- margin、padding、tint、hint不想再混乱了
- java多线程总结 (一)
- 1. (寄存器)STM32学习——概述
- JQuery AJAX $.get()方法
- Reorder List (Java)
- 20150101-回顾2014,展望2015
- 文件目录操作二----------目录创建删除显示