This snippet shows how you can manually split up a sentence using multiple delimiters. In this example, I split up this sentence: "Hello, my name is william!" into the following: "Hello" "my" "name" "is" "william" As you can see, the delimiters are not included ( " ,!" )

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// Returns true if text contains ch
inline bool TextContains(char *text, char ch) {
  while ( *text ) {
    if ( *text++ == ch )
      return true;

  return false;

void Split(char *text, char *delims, vector<string> &words) {
  int beg;
  for (int i = 0; text[i]; ++i) {

    // While the current char is a delimiter, inc i
    while ( text[i] && TextContains(delims, text[i]) )

    // Beginning of word
    beg = i;

    // Continue until end of word is reached
    while ( text[i] && !TextContains(delims, text[i]) )

    // Add word to vector
    words.push_back( string(&text[beg], &text[i]) );

int main() {
  vector<string> words;

  // Split up words
  Split( "Hello, my name is william!", " ,!", words );

  // Display each word on a new line
  for (size_t i = 0; i < words.size(); ++i)
    cout << words[i] << '\n';

8 Years
Discussion Span
Last Post by William Hemsworth

i believe there are better ways for doing it! such as using strtok()! for example.!


Perhaps read the description, it may not be the best way of doing it, but this is explaining how you can manually achive it, to understand how it works.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.