2

I need some help on making a function to split sentence into words and this function should work on sentence with different lengths.

Here is the sample code:

void spilt_sentence(string sentence)
{}
int main()
{
   std::string sentence1= "Hello everyone";
   std::string sentence2= "Hello I am doing stuff";
   split_sentence(sentence1);
   split_sentence(sentence2);
   return 0;
}

I saw someone use std::istringstream to get every words before each space but I don't really know how it works. It gives me error when I put std::istringstream ss(sentence); in the code. Also, I am using c++98 and I compile my program with cygwin. Any leads? Thank you.

Edit: The function will create a number of variables depending on how many words are there in the sentence.

Edit: I am actually working on a LinkedList program and what I am trying to do here is split sentence into words and then generate new nodes containing each word.

Here is the actual code (note: I modified it a little bit so it's not exactly the same as my actual one. Also I am not using struct for Node) and let's say sentence 1 is "Hello everyone" and sentence 2 is "Hello I am doing stuff".

The expected output will be:
linkedlist1:
"hello"<->"everyone"
linkedlist2:
"hello"<->"I"<->"am"<->"doing"<->"stuff"

inside LinkedList.cpp:

void LinkedList::add(std::string sentence)
{
   //breaks down the sentence into words
   std::istringstream ss(sentence);
   do
   {
       std::string word;
       ss >> word;

       //store them in nodes in a linkedlist
       Node* new_tail = new Node(word);
       if (size == 0)
       {
           head = new_tail;
           tail = new_tail;
       }
       else
       {
           new_tail->set_previous(tail);
           tail->set_next(new_tail);
           tail = new_tail;
       }
       new_tail = NULL;
       size++;

   }
   while(ss);
}

[FIXED]An error message pop up when I compile it, saying std::istringstream ss has default settings but the type is incomplete. What should I do?

error

Spooky
  • 39
  • 1
  • 9
  • Hi interesting, perhaps this answer might be of interest https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string – IronMan Mar 27 '20 at 01:50
  • @IronMan Thanks for the quick reply. I am having a look at it. I'll update the code once I figure it out. – Spooky Mar 27 '20 at 01:57
  • Maybe this is something for you: https://stackoverflow.com/a/9437426/391691 – Simson Mar 27 '20 at 02:30
  • Welcome to [so]. From the incomplete code you give, it is not possible to pinpoint the exact problem with the code. Please provide a [mre] to demonstrate the error you get. – L. F. Mar 27 '20 at 02:31
  • Sorry, I am not really good at making examples out of the program that I am working on. I'll change my code of the actual program a little bit and then show it in the post. – Spooky Mar 27 '20 at 03:07
  • Is there any particular reason you have to use C++98? A 22-year-old standard, revised 5 times by now (if you count C++20). – bitmask Mar 27 '20 at 04:56
  • @bitmask yes It's a project for university and I am restricted to use c++98. – Spooky Mar 27 '20 at 05:04
  • I know this is not helpful, but this makes so sad. Perhaps you should challenge your teachers or something. A university should not teach people to use outdated standards. – bitmask Mar 27 '20 at 15:53
  • yeah, the university do that so that they can reuse their old course material. But I think it is fine since I am a beginner at coding so I will follow along. Anyways, thank you everyone for the help and suggestions. The program is finally getting somewhere. Also, the solution provided by @Chestera did help me so I will mark it as the solution. – Spooky Mar 28 '20 at 05:30

1 Answers1

0

Here is the function using streams, this function will work only for vectors, you can't use this function for arrays, but if you want to, you can modify it for you. Here is the code and usage example

#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;

void split_sentence(const string& str, vector<string>& cont)
{
    istringstream iss(str);
    copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter(cont));


     //checking for punctuation marks and if found, we remove them from the word
     for(int i = 0, sz = cont.size(); i < sz; i++){
        string word = cont.at(i);
        for(int j = 0, len = word.length(); j < len; j++){
            if(ispunct(word[j])){
                cont.at(i) = word.substr(0, word.length() - 1);
            }
        }
     }
}

int main(){

    string sentence = "this is a test sentence for stackoverflow!";
    vector<string> words;

    split_sentence(sentence, words);

    for(int i = 0, sz = words.size(); i < sz; i++){
        cout<<words.at(i) << endl;
    }

    return 0;

}

And this is the output

this
is
a
test
sentence
for
stackoverflow

if you also want to print punctuation marks then remove double for loop in fucntion.

Chestera
  • 667
  • 2
  • 13
  • 22