Multiple ways to split a string in C++

In this tutorial, we are going to learn different ways to split a string in C++.

Here, we shall be using three ways to split a string.

  1. Using strtok() function
  2. Using istringstream and
  3. Making our own logic code.

Using strtok() function: Split a string in C++

We shall be using strtok() function to split the string. The function returns a pointer to the first letter of the token parsed at the first calling. Subsequent calling of the function parses or splits the rest of the string.  We print each token when the string is split.

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{ 
  system("cls");       //to clear the screen
  char string[30];
  cout << "Enter a string: ";
  gets_s(string);       //input the string
  char* p = strtok(string, " ");   //strtok() func. with delimeter " "
  while(p)
  {
    cout << p << endl;     //printing each token
    p = strtok(NULL, " ");
  }
  return 0;
}

Output:

Enter a string:
This is CodeSpeedy
This
is
CodeSpeedy

Using istringstream: Split a string in C++

In this method, we shall use istringstream to split the string. We first create an object of the class istringstream which copies the content of the string passed as argument. Every time the >> overloaded operator is used, it extracts the token, puts it in S (another string), which is then displayed on the screen.

#include <iostream>
#include<sstream>
#include <string.h>
using namespace std;
int main()
{
  system("cls");
  cout << "Enter a string : ";
  char string[30];
  gets_s(string);     //input the string

   istringstream iss(string);     //object 'iss' created
   while (iss)
   {
     char S[15];
     iss >> S;       
     cout << S << endl;     //The token is displayed

   }
   return 0;
}

Output:

Enter a string : This is CodeSpeedy
This
is
CodeSpeedy

Making your own logic to split a string

The most old school, tried and tested method is to apply the logic and make your own code.

In this code, I follow the following steps:

  1. Input the string and declare another string S for tokens
  2. Call a while loop until the string terminates
  3. Copy the elements of string into the new string S until the delimeter is met
  4. When the delimiter is met, at some S[j], assign null character to S[j+1] to terminate the array
  5. Print each token. continue the while loop until string doesn’t reach the end
#include <iostream>
using namespace std;
int main()
{
  system("cls");
  char string[30];
  cout << "Enter a string: ";
  gets_s(string);     //input the array
  
  char S[15];      //another array
  int i = 0, j = 0;
  while (string[i] != NULL)  //loop until the end of string
    {
      S[j] = string[i];
      if (S[j] == ' ')
      {
        S[j + 1] = '\0';     //to show the string terminates at S[j]
        cout << S << endl;   //Printing the token
        j = -1;
        
      }
      i++;
      j++;
  }
  cout << S << endl;      //the last token
  return 0;
}

Output:

Enter a string: This is CodeSpeedy
This
is
CodeSpeedy

Hope this was helpful. Enjoy Coding!

Also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *