?
Solved

c++ substr (2)

Posted on 2007-10-21
7
Medium Priority
?
698 Views
Last Modified: 2012-06-27
take a look at my question here: http://www.experts-exchange.com/Programming/Languages/CPP/Q_22907289.html

Problem is that i accept answer, but when i checked code it wont work correctly. I need just one word as output, but it gives output:
WORD: 1stWORD
WORD: 1stWORD 2ndWORD
and so on...
this time, i will share points who will help me ;)
last time, i gave all points to one guy, because i didn't knew how to give to few people.
0
Comment
Question by:moonskyland
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20118244
The problem is that you use 0 as starting point for the substr call every time ... You have to modify the starting point every time you call the substr function.

As I said in the other thread : you shouldn't have just taken his code, but should have tried to find the solution yourself ... you probably wouldn't have had this problem then :)

Try to understand what the code does. Take a look at this reference page for substr - it will help you :

        http://www.cplusplus.com/reference/string/string/substr.html

Also, did you get rid of the deprecated <iostream.h> include as I said in your other question ?
0
 
LVL 10

Accepted Solution

by:
peetm earned 375 total points
ID: 20118782
Your loop conditional needs to look something like this:

while((n = s.find(" ", i)) != string::npos)

Think about what types and initial values n and i might have.

Actually, a 'do' loop would probably work better.
0
 
LVL 7

Assisted Solution

by:lucky_james
lucky_james earned 375 total points
ID: 20121051
moonskyland.....you should have told me that the code is not working. i would be glad to help you. I saw your comment in the previous thread and this thread. :)

flaw in the logic : you need to recognise that you have got one word already rather than restarting from start of the string.

Anyway try this code:
     int i;
    string s;  
    cout << "Enter Sentence:\n";
    getline(cin, s);
      string s2(" ");
      s.append(s2);
    cout << "You entered:" << s << "\n";
      while(s.length())
      {
            for(i = 0; i<=s.length();i++)
            {
                  if (s[i]== ' ' )
                  {
                        s2 =  s.substr(0, i);
                        cout << s2 << "\n" ;
                        s.erase(0,i+1);
                  }
            }
      }
   
    system("PAUSE");
      return 0;



Hope it helps.

Infinity : hats off to you....guys like you think for the welfare of the community. I am posting code becoz now he can see the difference between the previous code and this one.


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:lucky_james
ID: 20121058
this time i have tried it hands on
so, more probability for you not saying 'some guy took away points and his code didnt work' :)

take care...
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 375 total points
ID: 20122318
>>>>                       s.erase(0,i+1);

erasing the extracted substring may solve the initial problem but is nevertheless inefficient and not needed here.

>>>> for(i = 0; i<=s.length();i++)
don't make i<=s.length() cause s[i] was not well-defined for i >= s.length(). It may work nevertheless cause std::string adds a zero character at end of string but actually that isn't a must and should be avoided. You also should see that  (s[i]== ' ' ) would not be true if s[i] is the zero character, so the last substring would not be recognized with loop lucky_james posted.

Better use something like that:

    s += ' ';   // we add a space character at end to get a final delimiter
    int npos = 0;
    int lpos = 0;
    while ((npos = (int)s.find_first_of(" \n\t\r", lpos)) != string::npos)
    {
           ...
           lpos = npos + 1;
    }

With that we find any 'whitespace' character like space, linefeed, carriagecontrol and tab. Furthermore we also find multi occurences of these, e. g. two spaces, what means that for the ... we could make a check like that:

           if (npos > lpos)  
           {
                 ...
           }

what prevents us from extracting an empty substring if two whitespace character were in sequence. The final replacement for the ... is now to get the substring between the two whitespace characters. We *know* that it begins at lpos cause lpos was npos+1 where (old) npos was a whitespace and it ends at (new) npos-1 cause we found the next whitespace char there.

         cout << "substring: " << s.substr(lpos, npos - lpos) << endl;

Regards, Alex



 
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 375 total points
ID: 20122364
>> Infinity : hats off to you....guys like you think for the welfare of the community. I am posting code becoz now he can see the difference between the previous code and this one.

Instead of posting code, explain what should be changed, and why. That way, the asker will have to think about it, and come up with his own way of implementing the suggested changes. It also makes sure that the asker understands the changes, which is what this is all about.

If you just post code, then the asker will usually just test if it works, and leave it at that without having learned a lot more than copy-pasting. Although that's a good thing to learn, it's not gonna help him/her in later assignments, and the problem he/she has with the current assignment will manifest itself again later.
Or in other words : you provided a short-term solution to finish his assignment, but you didn't provide a long-term solution to teach something that the asker can use in all his other assignments, and in his professional life.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20122401
Well, each of the change i made, i explained each change in the code.....furthermore, the code is the like limitless sky, you can always find a room to improve it....Alex is already on to it.  :)
Still, I honor ur valuable comments.....
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question