# Strings and Linked lists in C++



## [I.R.A]_FBi (Mar 19, 2011)

Working on a basic text editor in c++ using linked lists

i have a method that that accesses a class method called add that accepts words froma stringt for example "the aflac duck" gets broken into three items by insert and and get added to a link list by insert. running on the assumption so far that each word has 1 space between it.


```
void insert(string w1)
{
     string w2="";
     char c1;
     
     string::iterator it;
     for (it=w1.begin(); it < w1.end(); it++)
     {
         c1 = *it;
         
         if( c1 == ' ')
         {
             list.add(w2);
             w2="";
         }
         else
         {
             w2 = w2 + c1;
         }
     }
}
```

why if for example i do


```
insert(st1);
```

where st1 is a string do i get "the" when i do a readback

as opposed to if i use


```
insert("the aflac duck");

i do a readback and get the whole string ... (the aflac duck)


what am i doing wrong
```


----------



## Zyon (Mar 20, 2011)

You don't have to check every character yourself until it is a space, try using stringstream from C++'s standard library:

#include <sstream>

Create the stringstream object, initialise it with the input string, and then do a while loop as long as the stream has character (stringstream.good()) and perform a getline using ' ' (space) as delimiter, then add the string obtained from getline() to your linked list.

I can give you a sample completed code if you need it.

(Just to make it easier for you, getline() takes in input stream, string parameter to output line to, and optionally delimiter)


----------



## Kreij (Mar 20, 2011)

How are you telling it to display the separated string?
Could you inadvertantly be just displaying at the first string in the list and they are actaully all there?

Did you get my PM about converting to c-string and tokenizing?


----------



## [I.R.A]_FBi (Mar 20, 2011)

thx guys. i got ur pm kreij.

i tried tokenizing but to no avail


----------

