博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

单词逆序输出-I am a stdudent--stdudent a am I

Posted on 2011-10-20 11:45  ChessYoung  阅读(558)  评论(0编辑  收藏  举报
// Console1018.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <sstream>
#include <string>
using namespace std;
#define N 100

void ReverseWord(char *s, int low, int high);

int main()
{
char s[] = "I am a stdudent";

int low = 0;
int high = 0;
int i = 0;
int len = 0;
int lowFlags = 0;
int highFlags = 0;

while (s[len] != '\0')
{
len++;
}

ReverseWord(s, 0, len-1);

while (s[i] != '\0')
{
if (s[i] != ' ' && lowFlags == 0)
{
low = i;
lowFlags = 1;
}

if (s[i] == ' '&& highFlags == 0)
{
high = i - 1;
highFlags = 1;
}

if (lowFlags == 1 && highFlags == 1)
{
ReverseWord(s, low, high);
lowFlags = 0;
highFlags = 0;
}

++i;
}
cout << s << endl;
}

void ReverseWord( char *s, int low, int high )
{
int i = low;
int j = high;

while (i < j)
{
swap(s[i], s[j]);
++i;
--j;
}
}