C++ 反向数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1128985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:56:03  来源:igfitidea点击:

C++ Reverse Array

c++arraysreverse

提问by Dave Gamble

In C++, I need to:

在 C++ 中,我需要:

  • Read in a string from user input and place it into a char array [done]
  • Then pass that array to a function [done]
  • The function is supposed to reverse the order of characters [problem!]
  • Then, back in the main(), it displays that original array with the newly reversed characters.
  • 从用户输入中读取字符串并将其放入字符数组 [完成]
  • 然后将该数组传递给函数 [done]
  • 该函数应该颠倒字符的顺序[问题!]
  • 然后,回到 中main(),它显示带有新反转字符的原始数组。

I'm having trouble creating the function that actually does the reversing because I have some restrictions:

我在创建实际执行反转的函数时遇到问题,因为我有一些限制:

  • I cannot have any local array variables.
  • No pointers either
  • 我不能有任何局部数组变量。
  • 也没有指点

My function is only passing in the original array ie:

我的函数只传入原始数组,即:

void reverse(char word[])

EDIT:Here's my code base so far:

编辑:到目前为止,这是我的代码库:

void reverse(char word[]);

void main() 
{
  char word[MAX_SIZE];

  cout << endl << "Enter a word : ";
  cin >> word; 
  cout << "You entered the word " << word << endl;

  reverse(word); 

  cout << "The word in reverse order is " << word << endl;
}

void reverse(char myword[]) 
{
  int i, temp;
  j--;

  for(i=0;i<(j/2);i++) 
  {
    temp      = myword[i];
    myword[i] = myword[j];
    myword[j] = temp; 

    j--; 
  }
}

回答by Dave Gamble

Despite this looking quite homeworky, may I suggest:

尽管这看起来很家庭作业,但我可以建议:

void reverse(char word[])
{
    int len=strlen(word);
    char temp;
    for (int i=0;i<len/2;i++)
    {
            temp=word[i];
            word[i]=word[len-i-1];
            word[len-i-1]=temp;
    }
}

or, better yet, the classic XOR implementation:

或者,更好的是,经典的 XOR 实现:

void reverse(char word[])
{
    int len=strlen(word);
    for (int i=0;i<len/2;i++)
    {
        word[i]^=word[len-i-1];
        word[len-i-1]^=word[i];
        word[i]^=word[len-i-1];
    }
}

回答by Greg Hewgill

Since this is homework, I'll point you toward a solution without just giving you the answer.

由于这是家庭作业,我会为您指出解决方案,而不仅仅是给您答案。

Your reversefunction can modify the wordthat is passed in. One thing you'll need to know is how long the word is (so you'll know how many letters to reverse), you can get this from the strlen()function. If you're not permitted to use pointers, then you can use a local intindex variable.

您的reverse函数可以修改word传入的 。您需要知道的一件事是单词的长度(因此您将知道要反转多少个字母),您可以从strlen()函数中获取它。如果不允许使用指针,则可以使用局部int索引变量。

回答by cs11

//this program helps you to find maximum and minimum number in an array

//这个程序帮助你找到数组中的最大和最小数

#include<iostream.h>
#include<conio.h>
int main()
{int max;

    int a[5]={12,1,0,4,5};
 int min=a[0];
    int i=1;
    max=a[0];

    while(i<=4)
    {
              if (max < a[i])
              max=a[i];
              if (min > a[i])
              min=a[i];
              i++;

    }
              cout << min <<"\n";
              cout << max;
              getch();
              return 0;
}              

回答by Tom

If we are talking C-Strings, then your function should be

如果我们谈论的是 C-Strings,那么你的函数应该是

void reverse(char word[],size_t wordlen)

First answer from the same question (this is a dupe from Reverse a sentence in C?)

来自同一问题的第一个答案(这是来自Reverse a sentence in C的骗局

This doesn't do what you are looking for, but gets you quite close!

这不会做你正在寻找的东西,但会让你非常接近!

 int ReverseString(char *rev)
         {
            if(*rev!='##代码##')
            {
               ReverseString(rev + 1);
               putchar(*rev);//change this.
            }

            return 1;
         }

Credits to @devinb.

归功于@devinb。