当前位置:网站首页>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
边栏推荐
- 阿里云有奖体验:如何通过ECS挂载NAS文件系统
- Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
- 【网络原理的概念】
- 死锁的产生条件和预防处理[通俗易懂]
- You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
- Is private equity legal in China? Is it safe?
- [function recursion] do you know all five classic examples of simple recursion?
- 恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
- 寫一下跳錶
- 使用高斯Redis实现二级索引
猜你喜欢

Make this crmeb single merchant wechat mall system popular, so easy to use!

【C语言】指针进阶---指针你真的学懂了吗?
Klocwork 代码静态分析工具

The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization

OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验

软件缺陷静态分析 CodeSonar 5.2 新版发布

C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point

Intelligent software analysis platform embold

Measure the height of the building

CodeSonar网络研讨会
随机推荐
恶魔奶爸 指南帖——简易版
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
反诈困境,国有大行如何破局?
程序猿赚的那点钱算个P啊!
私募基金在中国合法吗?安全吗?
Solve the problem that the executable file of /bin/sh container is not found
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
npm uninstall和rm直接删除的区别
[function recursion] do you know all five classic examples of simple recursion?
Cantata9.0 | 全 新 功 能
Mongodb learn from simple to deep
机械臂速成小指南(十一):坐标系的标准命名
恶魔奶爸 A3阶段 近常速语流初接触
Postgresql数据库character varying和character的区别说明
How to choose financial products? Novice doesn't know anything
[matrix multiplication] [noi 2012] [cogs963] random number generator
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
Deep learning model compression and acceleration technology (VII): mixed mode
静态测试工具
Phoenix JDBC