单词翻转
来源:互联网 发布:喀秋莎软件中文版 编辑:程序博客网 时间:2024/04/30 10:28
#include<iostream>
#include<string>
using namespace std;
class ReverseWords{
public:
ReverseWords(string* wo):words(wo){}
void reverse_()
{
int length=words->size();
int begin=-1,end=-1;
for(int i=0;i<length;++i){
if(begin==-1&&words->at(i)==' ')//去掉 字符串最前面的空格
continue;
if(begin==-1)//记录第一个非空格字符出现的位置
{
begin=i;
continue;
}
if(words->at(i)==' ')//记录结束位置
end=i-1;
else if(i==length-1)
end=i;
else
continue;
reverse__(begin,end); //1.字母翻转
begin=-1,end=-1;
}
reverse__(0,length-1); //2.单词翻转
}
private:
void reverse__(int begin,int end) //
{
#include<string>
using namespace std;
class ReverseWords{
public:
ReverseWords(string* wo):words(wo){}
void reverse_()
{
int length=words->size();
int begin=-1,end=-1;
for(int i=0;i<length;++i){
if(begin==-1&&words->at(i)==' ')//去掉 字符串最前面的空格
continue;
if(begin==-1)//记录第一个非空格字符出现的位置
{
begin=i;
continue;
}
if(words->at(i)==' ')//记录结束位置
end=i-1;
else if(i==length-1)
end=i;
else
continue;
reverse__(begin,end); //1.字母翻转
begin=-1,end=-1;
}
reverse__(0,length-1); //2.单词翻转
}
private:
void reverse__(int begin,int end) //
{
while(begin<end)
{
char t=words->at(begin);
words->at(begin)=words->at(end);
words->at(end)=t;
++begin;
--end;
}
}
string* words;
};
int main(){
string s="I am a student.";
ReverseWords r(&s);
r.reverse_();
cout<<s<<endl;
return 0;
}
- 单词翻转
- 单词翻转
- 单词翻转
- 翻转单词
- 单词翻转
- 翻转单词
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 翻转单词
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- camshift算法(3)
- Emacs学习笔记 hour3 Getting Started with Emacs
- 什么是OpenCL?
- 探索C++的秘密之详解extern "C"
- 怎样把你的肾补起来!(原来我有黑眼圈,皮肤发黄时肾不好啊)!!世界上唯一有能力让时间扭转、青春永在的最强有力的器官,就是肾脏。
- 单词翻转
- JQuery Highcharts图表控件使用说明
- 状态栏
- hdu 1010 深搜+回溯
- PHP中界定符的使用
- JNI(Java Native Interface)在多线程中的运用
- 细说Cookie
- VC++ Type
- 通用计算时代来临 论CUDA与OpenCL的异同