当前位置:网站首页>Word inversion implements "suggestions collection"
Word inversion implements "suggestions collection"
2022-07-07 20:59:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
As the title , The code is as follows :
void reverse_words(std::string& original)
{
enum {
scan_normal,
scan_frag,
} state;
char buf[4];
memset(buf, 0, sizeof(buf));
int last = original.size() - 1;
int cur = original.size() - 1;
int n = original.size();
char* p = &original.front(); // scan ptr
char* movep = p; // move ptr
state = scan_normal;
int wfrag_len = 0;
int wlen = 0;
int movel = n;
int scan_ok;
while (n > 0) {
scan_ok = 1;
int end_frag = 0;
wfrag_len = 0;
// scan word until ' ' or word size >= word_buf
while (p[cur] != ' ') {
--cur;
if ( (wfrag_len = (last - cur)) >= sizeof(buf) ) {
scan_ok = 0;
if (scan_normal == state) {
state = scan_frag;
}
break;
}
}
if (state == scan_normal)
wlen = wfrag_len;
else
wlen += wfrag_len;
movel = n - wfrag_len;
if (movel <= 0 || n <= wlen)
break;
if (scan_ok) {
if (state == scan_frag) {
state = scan_normal;
end_frag = 1;
}
}
if (wfrag_len > 0) {
memcpy(buf, p + cur + 1, wfrag_len);
memmove(p + wfrag_len + scan_ok - end_frag, p, movel - (scan_ok - end_frag));
if (end_frag)
memmove(p + wlen, p + wlen - 1, n - wlen);
memcpy(p, buf, wfrag_len);
}
else { // skip empty
memmove(p + wlen + 1, p + wlen, movel - wlen - 1);
}
if (scan_normal == state) {
p[wlen] = ' ';
p += (wlen + 1);
n -= (wlen + 1);
cur = last = (n - 1);
}
else { // scan_frag
cur = last;
}
if (scan_ok || end_frag)
wlen = 0;
memset(buf, 0x0, sizeof(buf));
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116297.html Link to the original text :https://javaforall.cn
边栏推荐
- ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- 私募基金在中国合法吗?安全吗?
- How to choose fund products? What fund is suitable to buy in July 2022?
- 恶魔奶爸 指南帖——简易版
- Cantata9.0 | new features
- gridView自己定义做时间排版「建议收藏」
- easyui 日期控件清空值
- 如何挑选基金产品?2022年7月份适合买什么基金?
- [function recursion] do you know all five classic examples of simple recursion?
- 数值法求解最优控制问题(〇)——定义
猜你喜欢
MySQL约束之默认约束default与零填充约束zerofill
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System

How to meet the dual needs of security and confidentiality of medical devices?

Nebula importer data import practice

Cantata9.0 | 全 新 功 能
Klocwork code static analysis tool

How does codesonar help UAVs find software defects?

恶魔奶爸 B3 少量泛读,完成两万词汇量+

测量楼的高度

Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
随机推荐
npm uninstall和rm直接删除的区别
I Basic concepts
uva 12230 – Crossing Rivers(概率)「建议收藏」
特征生成
How to meet the dual needs of security and confidentiality of medical devices?
测量楼的高度
CodeSonar通过创新型静态分析增强软件可靠性
Static analysis of software defects codesonar 5.2 release
Codeforces Round #296 (Div. 2) A. Playing with Paper[通俗易懂]
Codesonar enhances software reliability through innovative static analysis
openGl超级宝典学习笔记 (1)第一个三角形「建议收藏」
Make this crmeb single merchant wechat mall system popular, so easy to use!
Tensorflow2.x下如何运行1.x的代码
CodeSonar如何帮助无人机查找软件缺陷?
恶魔奶爸 B2 突破语法,完成正统口语练习
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
Écrivez une liste de sauts
CodeSonar网络研讨会
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
Dachang classic pointer written test questions