句子逆序输出

#include 
#include 
#include 
#define ENDL '\0'
#define SPACE ' '

using namespace std;


void ReverseWord(char* p, char* q)//单词字母逆序
{
    while (p < q)
    {
        char t = *p;
        *p++ = *q;
        *q-- = t;
    }
}

char* ReverseSentence(char* s)//句子按单词逆序
{
    char* p = s;
    char* q = s;
    while (*q != ENDL)
    {
        if (*q == SPACE)
        {
            ReverseWord(p, q - 1);
            q++;
            p = q;
        }
        else
            q++;
    }
    ReverseWord(p, q - 1);
    ReverseWord(s, q - 1);

    return s;
}


void main()
{
    ofstream outfile("C:\\Users\\PC\\Desktop\\oooo.txt", ios::out);
    if (!outfile)
    {
        cerr << "open text.txt error" << endl;
        exit(0);
    }
    char c[80];
    cout << "enter a sentence:";
    cin.getline(c, 30);
    for (int i = 0; c[i] != 0; i++)
    {
        outfile.put(c[i]);

    }

    ReverseSentence(c);
    cout << c << endl;

    outfile.close();
    system("pause");
}

http://www.cnblogs.com/graphics/archive/2011/03/09/1977717.html

你可能感兴趣的:(初级)