简体   繁体   中英

How to reverse a string array in place using a swap function and pointers? (C++)

my assignment is to reverse a string array in place. I must use a swap function that swaps individual characters and this swap should be in a loop. Pass two character pointers to the swap function to manipulate the locations in memory. So here is what I have so far:

#include <iostream>
using namespace std;

void swap(char *a, char *b){ //swap function that accepts two char     pointers
    char temp = *a;          
    *a = *b;                 
    *b = temp;
}


int main()
{
    char array[5] = {'H','e','l','l','o'}; //string hello
    char *start = array[0]; //pointer for start of string
    char *end = array[array.length-1]; //pointer for end of string
    for (int i = 0, int j = array.length - 1; i < j; i++, j-- ){
        swap(start,end);
        start++;
        end--;
    }
    for(int i = 0; i < 5; i++){ //print reversed array to user
        cout << array[i];
    }

}

I have little experience with pointers so I may be using them incorrectly. Any help would be appreciated!

At lines

char *start = array[0];
char *end = array[array.length-1];

array[0] is a character, not a pointer. You can reference it to get a pointer with &array[0] , or take advantage of the fact that the name of an array can be used as a pointer to the first element like char *start = array .

When you did

for (int i = 0, int j = array.length - 1; i < j; i++, j-- ){

The problem is that there is no such thing as array.length in basic C arrays. Even if there is, it's called array.length() . You should either keep track of the length of an array separately or use sizeof(array) / sizeof(array[0]) . Note that this gives you the size of an array (compile time constant), not the length of a C-style string. Although a solution better than both of these methods is to just use an std::string , which keeps track of its length beside being much easier to use than C-style strings.

Also, you can't declare two variables in the same statement separated by a comma unless they are the same type, in which case you omit the type of the second variable like this: int i = 0, j = length .

Another thing, you should learn about using references to pass the arguments to your swap function so that you don't have to reference them all the time when using the function.

I think you were on the right track but you kind of lost it with your pointers.

#include <iostream>

using namespace std;

void swap(char *a, char *b) {
    char temp = *a;
    *a = *b;
    *b = temp;
}

int main()
{
    const unsigned int array_size = 5u;
    char array[] = { 'H','e','l','l','o' };

    for (int i = 0, j = array_size - 1; i < j; i++, j--) {
        swap(&array[i], &array[j]);
    }

    for (int i = 0; i < array_size; i++) { 
        cout << array[i];
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM