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
#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;
}
Comments
Leave a comment