反转数组的内容

时间:2015-12-01 15:32:38

标签: c++ arrays char sequence

所以我被告知要用c ++编写一个用户输入一个单词的程序,我必须把它变成一个数组,但问题是我必须反转这个单词:

#include <iostream>

using namespace std;

const int EOS = '.';
const int MAX = 30;

typedef char VectorC[MAX];

void showInversion (char word[MAX],int n)
{
    for (int i = n; i <= n-1 ; i--){
        cout << word[i];
    }
}
int main()
{
   VectorC word;
   char c = 'a';
   int i = 0, n = 0;

   cout << "ENTER SEQUENCE:" << endl;

   while (c!=EOS){
       word[i] = c = cin.get();
       i++;
       n++;   //length of the word
   }
   cout << "THE RESULT IS: " << showInversion(word,n);

   return 0;
}

输出示例:

输入SEQUENCE:

计算机。

结果是:RETUPMOC

1 个答案:

答案 0 :(得分:0)

要打印反转序列,一种简单的方法是从头到尾打印单词中的字符。

void showInversion (char word[MAX],int n)
{
    for (int i = n - 1; i >= 0 ; i--){
        cout << word[i];
    }
}