1

I used this function but it is wrong.

for (int i=0; i<sen.length(); i++) {
    if (sen.find (' ') != string::npos) {
        string new = sen.substr(0,i);
    }
cout << "Substrings:" << new << endl;
}

Thank you! Any kind of help is appreciated!

  • 3
    As a start, don't use 'new' as a variable name. It is already used in the language for something else. – Asaf Jan 19 '12 at 13:27
  • Oh so it is a reserved word. I see. Thank you! –  Jan 19 '12 at 13:29
  • @CandaceParker: Please tell us what exactly is wrong with your function. Does it not compile? Does it crash? Does is deliver bad results? – Björn Pollex Jan 19 '12 at 13:32
  • 1
    and you use `sen.find(' ')` which will always find the _same_ space, no matter how many times you call it. And you use `sen.substr(0,i)` which will return the whole of the string up to i instead of only the latest word. – Mr Lister Jan 19 '12 at 13:32
  • 2
    I think you should really start with [a good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), since you seem to be missing some very basic knowledge. Believe me, you're in for a world of hurt if you try to learn C++ without any good book. – Xeo Jan 19 '12 at 13:35
  • 1
    possible duplicate of [How to split a string in C++?](http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c) – Xeo Jan 19 '12 at 13:46
  • possible duplicate of [How do I tokenize a string in C++?](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – sbi Jan 19 '12 at 13:48

4 Answers4

1

new is a keyword in C++, so first step is to not use that as a variable name.

After that, you need to put your output statement in the "if" block, so that it can actually be allowed to access the substring. Scoping is critical in C++.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

First: this cannot compile because new is a language keyword.

Then you have a loop running through every character in the string so you shouldn't need to use std::string::find. I would use std::string::find, but then the loop condition should be different.

Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
0

No need to iterate over the string, find already does this. It starts to search from the beginning by default, so once we found a space, we need to start the next search from this found space:

std::vector<std::string> words;

//find first space
size_t start = 0, end = sen.find(' ');

//as long as there are spaces
while(end != std::string::npos)
{
    //get word
    words.push_back(sen.substr(start, end-start));

    //search next space (of course only after already found space)
    start = end + 1;
    end = sen.find(' ', start);
}

//last word
words.push_back(sen.substr(start));

Of course this doesn't handle duplicate spaces, starting or trailing spaces and other special cases. You would actually be better off using a stringstream:

#include <sstream>
#include <algorithm>
#include <iterator>

std::istringstream stream(sen);
std::vector<std::string> words(std::istream_iterator<std::string>(stream), 
                               std::istream_iterator<std::string>());

You can then just put these out however you like or just do it directly in the loops without using a vector:

for(std::vector<std::string>::const_iterator iter=
    words.begin(); iter!=words.end(); ++iter)
    std::cout << "found word: " << *iter << '\n';
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
0

This doesn't use substr and find, so if this is homework and you have to use that then this won't be a good answer... but I do believe it's the better way to do what you're asking in C++. It's untested but should work fine.

//Create stringstream and insert your whole sentence into it.
std::stringstream ss;
ss << sen;

//Read out words one by one into a string - stringstream will tokenize them
//by the ASCII space character for you.
std::string myWord;
while (ss >> myWord)
    std::cout << myWord << std::endl;  //You can save it however you like here.

If it is homework you should tag it as such so people stick to the assignment and know how much to help and/or not help you so they don't give it away :)

John Humphreys
  • 37,047
  • 37
  • 155
  • 255