`
standalone
  • 浏览: 596043 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

how to reverse the word sequence in a sentence?

    博客分类:
  • c++
 
阅读更多

For example, "Welcome you to Beijing" is reversed into "Beijing to you Welcome"

void ReverseString(char *s, int start, int end)
 2{
 3    while (start < end)
 4    {
 5        if (s[start] != s[end])
 6        {
 7            s[start] ^= s[end];
 8            s[end] ^= s[start];
 9            s[start] ^= s[end];
10        }
11                
12        start++;
13        end--;
14    }
15}
16
17
void ReverseByWords(char *s, int len, char seperator)
{
19    int start = 0, end = 0;
20
21    ReverseString(s, start, len - 1);
22
23    while (end < len)
24    {
25        if (s[end] != seperator)
26        {
27            start = end;
28
29            while (end < len && s[end] != seperator)
30                end++;
31            end--;
32
33            ReverseString(s, start, end);
34        }
        
          end++;

          }//while
} 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics