C++句子翻转(my hometown is hubei⇒hubei is hometown my)
来源:互联网 发布:淘宝网买药提交需求 编辑:程序博客网 时间:2024/06/16 00:06
本方法只适用于单词中间只有一个空格的情况!
思路:先将整个句子翻转,然后再翻转每一个单词。
留着自己日后需要的时候参考。
#include<iostream>#include<assert.h>#include<cstring>using namespace std;void swap_str(char* str, int start, int end){ assert(str!=NULL && start <= end); while (start < end) { char cTmp = str[start]; str[start] = str[end]; str[end] = cTmp; start++; end--; }}void reverse_word(char str[]){ int len = strlen(str); //翻转整个句子使之变成为iebuh si nwotemoh ym swap_str(str, 0, len-1); int nStart = 0; int nEnd = 0; //翻转iebuh si nwotemoh ym每一个单词 for (int i = 0; i < len; i++) { nEnd = i; if (str[nEnd] == ' ') { swap_str(str, nStart, nEnd-1); nStart = nEnd + 1; } }}int main(){ char str[] = "my hometown is hubei"; reverse_word(str); cout<<str<<endl; return 0;}
0 0
- C++句子翻转(my hometown is hubei⇒hubei is hometown my)
- My Hometown
- My Hometown
- my hometown
- My hometown (IELST speaking)
- 英语练习My HomeTown
- I love my hometown 福建●浦城
- Lovely Hometown
- It is my brithday
- It is my pass
- My Birthday Is Coming
- My Niece Is Born
- This is my frist
- My blog is start
- My favourite is java
- My blog is coming!
- Today is my brithday~~~~!
- where is my destination??
- Codeforces Round #369
- Java并发编程:深入剖析ThreadLocal
- VC++ GUID随机字符串生成
- 公开的海量数据集 Public Research-Quality Datasets
- Android_ScrollView嵌套ListView会出现的ListView底部会自动出现在屏幕上
- C++句子翻转(my hometown is hubei⇒hubei is hometown my)
- 设计模式-建造者
- leetcode:数学:Palindrome Number
- android TabLayout实现京东详情效果
- opencv+arduino进行物体点追踪
- 如何让Android TextView内容滚动
- 【Java 语言】Java 多线程 一 ( 线程启动 | 线程停止 )
- NOIP2009 Hankson的趣味题 [数论]
- Scipy-矩阵运算