hongfang

导航

 

leetcode

string 反转

Given s = "the sky is blue",
return "blue is sky the".

思路:字符从后往前遍历 遇到空格打印单词 单词的界限为两个空格之间的字符串,代码如下:

#include<string.h>
#include<iostream>
using namespace std;
# define N 100

int main()
{
    string A= "the sky is blue";
    int length,i,j,k;
    length=A.length();
    i=length-1;
    for(i=j=length-1;i>=0;i--)
      {
          if(A[i]==' ')
            {
                for(k=i+1;k<=j;k++)
                    cout<<A[k];
                cout<<' ';
                j=i-1;
            }
          if(i==0)
            {
                for(k=i;k<=j;k++)
                    cout<<A[k];
            }
      }
}
运行ok!

posted on 2016-06-28 17:55  hongfang  阅读(105)  评论(0)    收藏  举报