当前位置:网站首页>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
边栏推荐
- 如何挑选基金产品?2022年7月份适合买什么基金?
- easyui 日期控件清空值
- 恶魔奶爸 指南帖——简易版
- Update iteration summary of target detection based on deep learning (continuous update ing)
- 软件缺陷静态分析 CodeSonar 5.2 新版发布
- Data sorting in string
- ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- 2022年在启牛开中银股票的账户安全吗?
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- 寫一下跳錶
猜你喜欢
Klocwork 代码静态分析工具
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
Implement secondary index with Gaussian redis
Static analysis of software defects codesonar 5.2 release
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
CodeSonar通过创新型静态分析增强软件可靠性
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
SQL注入报错注入函数图文详解
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
万字总结数据存储,三大知识点
随机推荐
Lex & yacc of Pisa proxy SQL parsing
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统
Solve the problem that the executable file of /bin/sh container is not found
I Basic concepts
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
Klocwork code static analysis tool
软件缺陷静态分析 CodeSonar 5.2 新版发布
Implement secondary index with Gaussian redis
Cantata9.0 | new features
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
CodeSonar通过创新型静态分析增强软件可靠性
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
恶魔奶爸 B2 突破语法,完成正统口语练习
DataTable数据转换为实体
Flask1.1.4 Werkzeug1.0.1 源码分析:路由
私募基金在中国合法吗?安全吗?
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
使用枚举实现英文转盲文
刚开户的能买什么股票呢?炒股账户安全吗