# Thread: Help! Defining a Function

1. ## Help! Defining a Function

Hello,

What I had to do in the code is to define the function seperateWords so that it separates whole sentences/phrases. For instance "I need help" would come out as "I","need","help".

Could anyone please find the faults in the following solution?

Code:
```Given:

#include <iostream>

using namespace std;

int main (void) {
vector<string> lines, words;
lines.push_back("Darth Archos");
lines.push_back("is the");
lines.push_back("Lord of the Sith");
words = seperateWords(lines);
cout << "The seperated words are:" << endl;
for (unsigned i = 0; i < words.size(); i++) {
cout << words[i] << ", ";
}
cout << endl;
return 0;
}
vector <string> seperateWords (vector<string> lines) {
vector<string > words;

//Separating stuff goes here

string toFind = " "; 			//find whitespace
string replaceWith = "\n"; 		//replace with new line
}
for(unsigned i=0;i < lines.size(); i++) //loop through first string
{
<< lines[i] 				//read from first string
}
for(unsigned x=0;x < lines.size(); x++)	//loop through second string
{
<< lines[x]				//read from second string
{
for(unsigned y=0;y < lines.size(); y++)	//loop through third string
{
<< lines[y]				//read from third string
}
return words;
}```

2. Yup, you need to put
Code:
`vector<string> seperateWords (vector<string>);`
above main().

3. Well, this ain't C++

What are you trying to do: take a string ("I need help") and break it into three strings or do you want to keep it as one string and simply replace spaces with newlines, so it becomes "I\nneed\nhelp"?

If it is the second, then this algorithm might help: replace_copy

For example, replacing lower-case a's with upper-case A's:
Code:
```#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
string s1("I have a dream");
string s2;
replace_copy(s1.begin(), s1.end(), back_inserter(s2), 'a', 'A');
cout << s1 << '\n' << s2 << '\n';
}```