当前位置:网站首页>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
边栏推荐
- [matrix multiplication] [noi 2012] [cogs963] random number generator
- Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
- 上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
- Is private equity legal in China? Is it safe?
- 私募基金在中国合法吗?安全吗?
- [paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
- Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
- 使用高斯Redis实现二级索引
- Klocwork 代码静态分析工具
- FTP steps for downloading files from Huawei CE switches
猜你喜欢

Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance

Cantata9.0 | new features

Tensorflow2.x下如何运行1.x的代码

Mongodb learn from simple to deep

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation

Dachang classic pointer written test questions

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

Implement secondary index with Gaussian redis
SQL注入报错注入函数图文详解

软件缺陷静态分析 CodeSonar 5.2 新版发布
随机推荐
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
Cantata9.0 | new features
【函数递归】简单递归的5个经典例子,你都会吗?
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
开户必须往账户里面赚钱吗,资金安全吗?
华为CE交换机下载文件FTP步骤
程序猿赚的那点钱算个P啊!
Lex & yacc of Pisa proxy SQL parsing
Intelligent transportation is full of vitality. What will happen in the future? [easy to understand]
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
Is it safe to open a stock account at present? Can I open an account online directly.
Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
DataTable数据转换为实体
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
object-c编程tips-timer「建议收藏」
【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
Tensorflow2.x下如何运行1.x的代码
[matrix multiplication] [noi 2012] [cogs963] random number generator
MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」