[编程题]字符串分隔
来源:互联网 发布:分时量比曲线源码 编辑:程序博客网 时间:2024/06/08 17:22
题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(输入2次,每个字符串长度小于100)
输出描述:
输出到长度为8的新字符串数组
输入例子:
abc
123456789
输出例子:
abc00000
12345678
90000000
思路解答:
#include<iostream>#include<vector>#include<string>using namespace std;int main(){vector<string> strarr;string str;while(getline(cin,str)){ int len =str.size(); int num=len%8; if(num!=0) { str.insert(str.end(),8-num,'0'); } int count=str.size()/8; for(int i=0;i<count;i++) { string str_tmp=str.substr(8*i,8); strarr.push_back(str_tmp); }}for(int i=0;i<strarr.size();i++){ cout<<strarr[i]<<endl;} return 0;}
注意:
1.substr的用法
string substr (size_t pos = 0, size_t len = npos) const;//从指定位置返回指定长度的子字符串
例题
// string::substr#include <iostream>#include <string>int main (){ std::string str="We think in generalities, but we live in details."; // (quoting Alfred N. Whitehead) std::string str2 = str.substr (3,5); // "think" std::size_t pos = str.find("live"); // position of "live" in str std::string str3 = str.substr (pos); // get from "live" to the end std::cout << str2 << ' ' << str3 << '\n'; return 0;}
2.insert的用法
string (1) string& insert (size_t pos, const string& str);substring (2) string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);c-string (3) string& insert (size_t pos, const char* s);buffer (4) string& insert (size_t pos, const char* s, size_t n);fill (5) string& insert (size_t pos, size_t n, char c); void insert (iterator p, size_t n, char c);single character (6) iterator insert (iterator p, char c);range (7) template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last);
3.错误的地方
int count=str.size()/8;一开始写的是int count=len/8;
0 0
- [编程题]字符串分隔
- [编程题]字符串分隔
- OJ题:字符串分隔
- c语言编程--字符串分隔 strtok
- 算法题:字符串分隔填充
- 分隔字符串
- 字符串分隔
- 字符串分隔
- 字符串分隔
- 字符串分隔
- 分隔字符串
- 字符串分隔
- 分隔字符串
- 字符串分隔
- 字符串分隔
- 字符串分隔
- 字符串分隔
- 字符串分隔
- Codeforces Round #371 (Div. 2)B. Filya and Homework
- iOS10配置须知
- HDU 1754 单点更新 区间求和 zkw线段树 + 递归线段树
- lightoj 1110 - An Easy LCS (LCS输出路径)
- [Javascript]对象数组排序
- [编程题]字符串分隔
- 详解GPS欺骗:针对导航系统的「致命攻击」
- Mat类型中的CV_8UC3、CV_32FC3以及对应的迭代器模板参数Vec3b,Vec3f的一点自己的理解
- Codeforces #371(Div.2)B. Filya and Homework【思维】
- IDEA git github操作
- 面试装13大法合集
- java不同数据类型在内存中的存储方式
- andorid 自定义View 命名空间 长话短说
- iOS 制作圆形图片