Answer to Question #201442 in C++ for shuvo

Question #201442

Write a function reverse that takes an array of strings as an argument and reverses it. The function does not return a value. Please do not create a new array. Modify the array parameter.

#include

#include

using namespace std;

void reverse_string(string s[], int size);

void print_array(const string s[], int size);

int main()

{

string words[] = {"Up", "above", "the", "world", "so", "high", "like", "a", "diamond", "in", "the", "sky"};

const int LENGTH = 12;

reverse_string(words, LENGTH);

print_array(words, LENGTH);

return 0;

Should print: sky the in diamond a like so world the above Up


1
Expert's answer
2021-06-01T02:58:29-0400
#include <iostream>
#include <string>
using namespace std;

void reverse_string(string s[], int size)
{
    for (int i=0; i<size/2; ++i) {
        string t = s[i];
        s[i] = s[size-1-i];
        s[size-1-i] = t;
    }
}

void print_array(const string s[], int size) 
{
    for (int i=0; i<size; ++i) {
        cout << s[i] << " ";
    }
    cout << endl;
}

int main()
{
    string words[] = {"Up", "above", "the", "world", "so", "high", "like", "a", "diamond", "in", "the", "sky"};
    const int LENGTH = 12;

    reverse_string(words, LENGTH);
    print_array(words, LENGTH);

    return 0;
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment