views:

1786

answers:

3
+1  Q: 

C++ Reverse Array

In C++, I need to read in a string from user input and place it into a char array [done]

Then, I need to pass that array to a function [done]

That function is supposed to reverse the order of characters [problem!]

Then after, back in the main(), it displays that original array with the newly reversed characters.

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: So far he has

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--; 
  }
}

HELP please!!

A: 

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

void reverse(char word[],size_t wordlen)

First answer from the same question (this is a dupe from http://stackoverflow.com/questions/1080449/reverse-a-sentence-in-c/1080463#1080463)

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

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

            return 1;
         }

Credits to @devinb.

Tom
Sorry, that is another restriction I forgot to mention - not supposed to use pointers either.
This doesn't return the reversed array, as requested by the question.
Greg Hewgill
@Greg Where does it say that must return reversed array?
Tom
@Tom: "back in the main(), it displays that original array with the newly reversed characters"
Greg Hewgill
ok, but its not "return " as in the return statement right?
Tom
I'll leave that for you to take up with @dazedandconfused.
Greg Hewgill
@Greg: Question states: void reverse(char word[])
Tom
Correct. Below is what I have so far. It doesn't actually build unless I pass an int along with the char (for j). When I do that though, it only swaps the first and last characters.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--; }}
Ive put this into your question. Do that in the future, so that everyone sees your advance (edit your question)
Tom
+3  A: 

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:

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];
    }
}
Dave Gamble
Thanks Dave, this really helps.
@dazedandconfused: I'm sure it does. What did you learn?
Greg Hewgill
@dazedandconfused: I've seen this question in C++ related job interviews. I agree with Greg. It is important you understand how his solution works. On a related note, in the real world one could simply use the standard reverse algorithm to reverse the order of characters in the array, i.e. std::reverse(word, word + strlen(word)). I wouldn't give that simple solution to your teacher, however. :)
Void
Using XOR will be far slower than swapping using a temp object.
Yacoby
+2  A: 

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

Your reverse function can modify the word that 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 int index variable.

Greg Hewgill
So, should I convert my char array into a string to use the strlen() function?
A string is exactly the same as a character array. You can call strlen(word).
Greg Hewgill
Thanks Greg, I was getting thrown off by a 'warning' that it was giving me. It was building just fine.