当前位置:网站首页>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
边栏推荐
- 使用高斯Redis实现二级索引
- 恶魔奶爸 A0 英文零基础的自我提升路
- sqlHelper的增删改查
- How to choose fund products? What fund is suitable to buy in July 2022?
- Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
- [award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
- MySQL storage expression error
- CodeSonar通过创新型静态分析增强软件可靠性
- Intelligent software analysis platform embold
- Ubuntu安装mysql8遇到的问题以及详细安装过程
猜你喜欢
Klocwork 代码静态分析工具
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
智能软件分析平台Embold
How to meet the dual needs of security and confidentiality of medical devices?
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
【C语言】指针进阶---指针你真的学懂了吗?
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
Mongodb learn from simple to deep
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
How to meet the dual needs of security and confidentiality of medical devices?
随机推荐
object-c编程tips-timer「建议收藏」
guava多线程,futurecallback线程调用不平均
Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
软件缺陷静态分析 CodeSonar 5.2 新版发布
MySQL约束之默认约束default与零填充约束zerofill
Ubuntu安装mysql8遇到的问题以及详细安装过程
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
Tensorflow2. How to run under x 1 Code of X
国家正规的股票交易app有哪些?使用安不安全
解决使用uni-app MediaError MediaError ErrorCode -5
FTP steps for downloading files from Huawei CE switches
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
部署、收回和删除解决方式—-STSADM和PowerShell「建议收藏」
AADL inspector fault tree safety analysis module
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
Static analysis of software defects codesonar 5.2 release
刚开户的能买什么股票呢?炒股账户安全吗
How to meet the dual needs of security and confidentiality of medical devices?