-2

I need a code that splits string texts like:

string part1 = "Hi i study computer science in a college";

Then, I want it to be added into my linked list.

Node1: "Hi"
Node2: "i"
Node3: "study"
Node4: "computer"
... 

I took a look but I couldn't find any source to do it. So can anyone help me?

David G
  • 94,763
  • 41
  • 167
  • 253
Egow487
  • 19
  • 3
  • 2
    That's a problem to solve. What progress have you made so far (aside from trying to copy someone else's "source to do it")? – JaMiT Dec 26 '19 at 22:54
  • 1
    Can you tell which approach have you tried already?, some code, some steps you followed. Without that you should know that there is a lot of ways of solving that problem and will greatly depends on what are you using to solve this problem. – carpinchosaurio Dec 26 '19 at 23:21
  • @Egow487 No, I cannot see that you created your own code. Where can I see your code? (It should by in your question, but it is not.) – JaMiT Dec 27 '19 at 00:44
  • It was added to answers almost a century ago – Egow487 Dec 27 '19 at 02:02
  • 1
    @Egow487 Oh, I see now that one of the answers is yours. I had not noticed that. Which beings up a good point: You should expect reactions to your question to be based on just your question. While it is valid to answer your own question, your question should be able to stand on its own. If someone needs to read your answer to understand your question, then your question is incomplete. (Besides, I do not see any answers written before 1969, half a century ago, a bare minimum for "almost a century ago". You undermine yourself when you use such hyperbole.) – JaMiT Dec 27 '19 at 02:43
  • Just i was confused, anyways. Thanks for your helps or what you tried to do. Have a good one :) – Egow487 Dec 27 '19 at 02:50

3 Answers3

0

Sure, let me do your homework:

using It = std::istream_iterator<std::string>;
std::list<std::string> list(It(std::istringstream(part1) >> std::ws), It());
std::for_each(begin(list), end(list),
    [i=0](auto const& s) mutable { std::cout << “Node” << ++i << “\n”; });

Inconveniently it uses too much magic to be an acceptable solution to your teacher.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
-1

I tried it myself with a complicated algorithm. here's the code

string  metin[500];
string a;
getline(cin,a);
int pos = 0;
string str1;
int elemans = 0;
for (int i = 0;i<=a.length();i++)
{
    if(a[i] == ' '|| i == a.length())
    {
        for(int j=pos; j<i;j++)
        {

            str1 = str1 + a[pos];
            pos++;


        }

        pos = i+1;

        metin[elemans] = str1;
        elemans++;
        str1 = "";
    }

}
cout << str1;
cout << a.length() << endl;
for(int i = 0; i<elemans;i++)
{

    cout << metin[i] << endl;
}

It's string split..

you can add for loop and addnode(metin[i]) you can do it yourselves.

Egow487
  • 19
  • 3
-1

If this is for homework, please do the exercise. If you skip the simple stuff you will never learn.

However, if you are genuinely interested in learning more, it is helpful to know that this kind of operation is known as "tokenizing". Google it. You may find more information than you can understand, but it will improve your knowledge of the theories.

A good discussion of it can be found here How do I tokenize a string in C++?

Tiger4Hire
  • 1,065
  • 5
  • 11
  • Supplying a term to search for is helpful, but that does not really constitute an answer by itself. An answer should include (some of) the information found by searching. – JaMiT Dec 26 '19 at 22:52
  • I Didn't skip it, just teacher wants what he taught us. And i already know that split method. But, i was looking for an algorithm for split, not any function – Egow487 Dec 26 '19 at 23:12
  • There are many approaches. Probably the most advance is to use a trie (regular expressions use this). The simplest is a loop that either pushes an empty string onto a vector or pushes the character on the the end of the last string in that vector. If you follow the link, there are at least 6 approaches listed. – Tiger4Hire Dec 26 '19 at 23:48
  • Thanks, i knew token split method. I think most useful method is token split – Egow487 Dec 27 '19 at 00:03