how to separate a word from a string

hi

hope yall are well.
how would i separate each word from a string?

let's say:
string sentence is initialized with "This is a test string."
how would i separate each word and then print them?

i was thinking something like finding the first space then extract that word and then erase it.
i have some code that needs adjustments.

1
2
3
4
5
6
7
8
9
10
11

while (input != ".")
	{
		word= input.find(" ");
		for (i = 0; i < word; f++)
		{
			cout << input[f];
			input.erase(word, input.length); //this is giving me some error.
                                                                        //also we can't use pointer yet.
		}	
	}

Last edited on
You might want to consider using a stringstream and extract each substring into a temporary string for printing or inserting into a vector.
hi jlb,
thanks for your reply.
we haven't learned stringstream...
If you've learned to use cin/cout then you've learned how to use a stringstream, the only thing you need to do is create the stringstream with your source string and then use the extraction operator>> to extract the words into some other string.

@gongong

It is as @jlb said. Most likely, they are not going to teach you the standard library at all besides ones very common ones like vector or string. It is up to you to learn them and utilize them. Unless your professor explicitly states that you aren't able to use something they have not taught, I do not see why not (if they do, I find it very ridiculous that they would do that).

There are many different ways how to do this. Here are some examples on how to do this.

Via sstream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

void extractString(std::string s) {
  std::istringstream is(s);
  std::string dummyString;
  while(is >> dummyString) {
    std::cout << dummyString << std::endl;
  }
  std::cout << std::endl;
}

int main() {

  extractString("This is a test string.");
  extractString("Hello there brown cow.");
  extractString("These wings are spicy.");
  extractString("There isn't a meeting until 14:30.");

  return 0;
}


Non sstream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

void extractString(std::string s) {
  while(s.find(" ") != std::string::npos) {
    size_t pos = s.find(" ");
    std::string dummyString = s.substr(0, pos);
    std::cout << dummyString << std::endl;
    s = s.substr(pos+1, s.size());
  }
  std::cout << s << std::endl << std::endl;
}

int main() {
  extractString("This is a test string.");
  extractString("Hello there brown cow.");
  extractString("These wings are spicy.");
  extractString("There isn't a meeting until 14:30.");
  return 0;
}
The point of homeworks like this is NOT to master C++ library magic, it is to understand how to manipulate array data.

Use a loop and find the spacea in the string. Every time you find a space, you have found the end of a word. Extract that from between the current index and the last index. Pay special attention to the beginning and end of the string.

Good luck!
Just because you have to print words doesn't mean you have to extract them. You can print the words a character at a time. Here's a solution using a simple state machine. This extracts words from cin, but can be easily modified to extract them from a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>

int
main()
{
    char ch;			// current character
    bool inWord=false;		// was the last character part of a word?
	
    while (std::cin.get(ch)) {
	if (std::isalpha(ch)) {
	    std::cout << ch;
	    inWord = true;	// indicate you're now in a word in case you weren't
	} else {
	    if (inWord) {
		// You've reached the end of a word
		std::cout << '\n';
		inWord = false;
	    }
	}
    }
}


The really hard part of this assignment is deciding what's a word. Fiji885 gives a good test case: "There isn't a meeting until 14:30." Is "isn't" a word? What about "14:30?" Since this is the beginner's forum, I wouldn't dwell on the details, except to document in your comments exactly how you define a word.
Topic archived. No new replies allowed.