Skip to main content
deleted 1 characters in body
Source Link
sepp2k
  • 9k
  • 2
  • 38
  • 51

Again this should just be string sentence = ("seattle is good";good");. No reason to use char*.

Again this should just be string sentence = "seattle is good";. No reason to use char*.

Again this should just be string sentence("seattle is good");. No reason to use char*.

deleted 1 characters in body
Source Link
sepp2k
  • 9k
  • 2
  • 38
  • 51
#include <string>
#include <iostream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::reverse;

// Splits a sentence by spaces into a vector of words
void extract(const string& sentence, vector<string>& words)
{
    size_t pos = 0;
    while(pos != string::npos)
    {  
        // Find position of next space
        int next_space = sentence.find(" ", pos);
        // Store everything up to next space in the words vector
        words.push_back( sentence.substr(pos, next_space - pos));

        // Continue at the next character which is not a space.
        pos = sentence.find_first_not_of(" ", next_space);
    }
}

// Prints the strings in the vector separated by spaces
void print_strings(const vector<string>& strings)
{
    for(size_t i = 0; i < strings.size(); i++) {
        cout << strings[i] << " ";
    }
    cout << endl;
}

int main()
{
    string sentence = ("seattle is good";good");
    vector<string> words;
    extract(sentence, words);
    reverse(words.begin(), words.end());
    print_strings(words);
}
#include <string>
#include <iostream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::reverse;

// Splits a sentence by spaces into a vector of words
void extract(const string& sentence, vector<string>& words)
{
    size_t pos = 0;
    while(pos != string::npos)
    {  
        // Find position of next space
        int next_space = sentence.find(" ", pos);
        // Store everything up to next space in the words vector
        words.push_back( sentence.substr(pos, next_space - pos));

        // Continue at the next character which is not a space.
        pos = sentence.find_first_not_of(" ", next_space);
    }
}

// Prints the strings in the vector separated by spaces
void print_strings(const vector<string>& strings)
{
    for(size_t i = 0; i < strings.size(); i++) {
        cout << strings[i] << " ";
    }
    cout << endl;
}

int main()
{
    string sentence = "seattle is good";
    vector<string> words;
    extract(sentence, words);
    reverse(words.begin(), words.end());
    print_strings(words);
}
#include <string>
#include <iostream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::reverse;

// Splits a sentence by spaces into a vector of words
void extract(const string& sentence, vector<string>& words)
{
    size_t pos = 0;
    while(pos != string::npos)
    {  
        // Find position of next space
        int next_space = sentence.find(" ", pos);
        // Store everything up to next space in the words vector
        words.push_back( sentence.substr(pos, next_space - pos));

        // Continue at the next character which is not a space.
        pos = sentence.find_first_not_of(" ", next_space);
    }
}

// Prints the strings in the vector separated by spaces
void print_strings(const vector<string>& strings)
{
    for(size_t i = 0; i < strings.size(); i++) {
        cout << strings[i] << " ";
    }
    cout << endl;
}

int main()
{
    string sentence("seattle is good");
    vector<string> words;
    extract(sentence, words);
    reverse(words.begin(), words.end());
    print_strings(words);
}
Added code for final program
Source Link
sepp2k
  • 9k
  • 2
  • 38
  • 51

If I were to write this program, I might do it like this:

#include <string>
#include <iostream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::reverse;

// Splits a sentence by spaces into a vector of words
void extract(const string& sentence, vector<string>& words)
{
    size_t pos = 0;
    while(pos != string::npos)
    {  
        // Find position of next space
        int next_space = sentence.find(" ", pos);
        // Store everything up to next space in the words vector
        words.push_back( sentence.substr(pos, next_space - pos));

        // Continue at the next character which is not a space.
        pos = sentence.find_first_not_of(" ", next_space);
    }
}

// Prints the strings in the vector separated by spaces
void print_strings(const vector<string>& strings)
{
    for(size_t i = 0; i < strings.size(); i++) {
        cout << strings[i] << " ";
    }
    cout << endl;
}

int main()
{
    string sentence = "seattle is good";
    vector<string> words;
    extract(sentence, words);
    reverse(words.begin(), words.end());
    print_strings(words);
}

If I were to write this program, I might do it like this:

#include <string>
#include <iostream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::reverse;

// Splits a sentence by spaces into a vector of words
void extract(const string& sentence, vector<string>& words)
{
    size_t pos = 0;
    while(pos != string::npos)
    {  
        // Find position of next space
        int next_space = sentence.find(" ", pos);
        // Store everything up to next space in the words vector
        words.push_back( sentence.substr(pos, next_space - pos));

        // Continue at the next character which is not a space.
        pos = sentence.find_first_not_of(" ", next_space);
    }
}

// Prints the strings in the vector separated by spaces
void print_strings(const vector<string>& strings)
{
    for(size_t i = 0; i < strings.size(); i++) {
        cout << strings[i] << " ";
    }
    cout << endl;
}

int main()
{
    string sentence = "seattle is good";
    vector<string> words;
    extract(sentence, words);
    reverse(words.begin(), words.end());
    print_strings(words);
}
added 94 characters in body; added 96 characters in body
Source Link
sepp2k
  • 9k
  • 2
  • 38
  • 51
Loading
Source Link
sepp2k
  • 9k
  • 2
  • 38
  • 51
Loading