40.4k views
1 vote
write a complete C++ program. Create a function called string cat(vector words). Your function should use a for loop and string concatenation to return the words separated by spaces. Run the function multiple times in main with various vectors of words. At least one of your vectors must be only strings that look like integers ("1", "-15", etc).

1 Answer

5 votes

Final answer:

The C++ program includes a function 'string cat(vector words)' that concatenates a list of strings with spaces. It provides a demonstration of using the function with various vectors, including one with number-like strings.

Step-by-step explanation:

The question asks for a C++ program that includes a function called string cat(vector words). This function will concatenate a sequence of words, with spaces between each word, using a for loop.

Here's a simple C++ program that demonstrates this:

#include
#include
#include
using namespace std;
string cat(vector words) {
string result;
for (size_t i = 0; i < words.size(); ++i) {
result += words[i];
if (i < words.size() - 1) {
result += " ";
}
}
return result;
}
int main() {
vector vec1 = {"Hello", "there", "world"};
vector vec2 = {"1", "-15", "42"};
vector vec3 = {"C++", "is", "fun"};
cout << cat(vec1) << endl;
cout << cat(vec2) << endl;
cout << cat(vec3) << endl;
return 0;
}
User Greg Whitfield
by
8.8k points