Chinaunix首页 | 论坛 | 博客
  • 博客访问: 261100
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-04-20 09:55:59

例如:This is a sample ->SAMPLE A IS tHIS

  1. #include<iostream>
  2. #include<string>
  3. #include <stack>
  4. using namespace std;
  5. class Transform
  6. {
  7. public:
  8.     string trans(string& s, int n)
  9.     {
  10.         //首先转换大小写
  11.         for(int i = 0;i < n;++i)
  12.             {
  13.                 if(s[i] >= 'a' && s[i] < 'z')
  14.                 {
  15.                     s[i] -= 32;
  16.                 }
  17.                 else if(s[i] > 'A' && s[i] < 'Z')
  18.                 {
  19.                     s[i] += 32;
  20.                 }
  21.         }
  22.         //将字符串整个逆转
  23.         int start = 0,end = n - 1;
  24.         while(start < end)
  25.         {
  26.             swap(s[start],s[end]);
  27.             start++;
  28.             end--;
  29.         }
  30.         //把单词逆转回来
  31.         string ret;
  32.         stack<char> sk;
  33.         int index = 0;
  34.         while(index < n)
  35.         {
  36.             while(index < n&&s[index] != ' ')//注意这里判断时为了防止下标越界,要加上判断下标的条件
  37.             {
  38.                 sk.push(s[index]);
  39.                 ++index;
  40.             }
  41.             ++index;
  42.             while(!sk.empty())
  43.             {
  44.                 ret += sk.top();
  45.                 sk.pop();
  46.             }
  47.             ret += ' ';
  48.         }
  49.         cout<<ret;
  50.         return ret;
  51.     }
  52. };
  53. int main()
  54. {
  55.     Transform t;
  56.     string s("This is a sample");
  57.     t.trans(s,16);

  58.     return 0;
  59. }

阅读(1443) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

enenshiwo2016-05-07 08:48:40

狼行China:#include<iostream>
using namespace std;
#include<assert.h>
#include<string>
class Transform {
public:
 string trans(string s, int n) {
  // write code here
  assert(n >= 1 && n <= 500);
  string ret;
  int finish = n;
  while (finish)
  {
   finish--;
   if (s[finish] >=&nbs

不全啊。。。咋只有一半

回复 | 举报

狼行China2016-04-27 08:35:33

一次遍历解决。那天我写的

狼行China2016-04-27 08:35:08

#include<iostream>
using namespace std;
#include<assert.h>
#include<string>
class Transform {
public:
 string trans(string s, int n) {
  // write code here
  assert(n >= 1 && n <= 500);
  string ret;
  int finish = n;
  while (finish)
  {
   finish--;
   if (s[finish] >=&nbs