How to Split strings in C++?

This topic will discuss how we can split given strings into a single word in the C++ programming language. When we divide a group of words or string collections into single words, it is termed the split or division of the string. However, splitting strings is only possible with some delimiters like white space ( ), comma (,), a hyphen (-), etc., making the words an individual. Furthermore, there is no predefined split function to divide the collection of strings into an individual string. So, here we will learn the different methods to split strings into a single one in C++.

How to Split strings in C++

Different method to achieve the splitting of strings in C++

  1. Use strtok() function to split strings
  2. Use custom split() function to split strings
  3. Use std::getline() function to split string
  4. Use find() and substr() function to split string

Use strtok() function to split strings

strtok(): A strtok() function is used to split the original string into pieces or tokens based on the delimiter passed.

Syntax

In the above syntax, a strtok() has two parameters, the str, and the delim.

str: A str is an original string from which strtok() function split strings.

delim: It is a character that is used to split a string. For example, comma (,), space ( ), hyphen (-), etc.

Return: It returns a pointer that references the next character tokens. Initially, it points to the first token of the strings.

Note: A strtok() function modifies the original string and puts a NULL character ('\0') on the delimiter position on each call of the strtok() function. In this way, it can easily track the status of the token.

Program to split strings using strtok() function

Let's consider an example to split string in C++ using strtok() function.

Program.cpp

Output

Enter a string:
Learn how to split a string in C++ using the strtok() function.

 Split string using strtok() function:
Learn
how
to
split
a
string
in
C++
Using
the
strtok()
function.

Program to use custom split() function to split strings

Let's write a program to split sequences of strings in C++ using a custom split() function.

Program2.cpp

Output

The split string is:
 i : 0 Program
 i : 1 to
 i : 2 split
 i : 3 strings
 i : 4 using
 i : 5 custom
 i : 6 split
 i : 7 function.

Use std::getline() function to split string

A getline() function is a standard library function of C++ used to read the string from an input stream and put them into the vector string until delimiter characters are found. We can use std::getline() function by importing the <string> header file.

Syntax

It has three parameters:

str: A str is a variable that stores original string.

token: It stores the string tokens extracted from original string.

delim: It is a character that are used to split the string. For example, comma (,), space ( ), hyphen (-), etc.

Program to use getline() function to split strings

Let's consider an example to split strings using the getline() function in C++.

Program3.cpp

Output

Welcome to the JavaTpoint and Learn C++ Programming Language.
Welcome
to
the
JavaTpoint
and
Learn
C++
Programming
Language.

Program to split the given string using the getline() function

Let's consider an example to split a given string in C++ using the getline() function.

Program4.cpp

Output

Your given string is: Learn How to split a string in C++
Learn
How
to
split
a
string
in
C++

Use find() and substr() function to split strings

Let's write a program to use find() function and substr() function to split given strings in C++.

Program4.cpp

Output

Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++
How
to
split
a
string
using
find()
and
substr()
function
in
C++

In the above program, we use a find() function inside the loop to repeatedly find the occurrence of the delimiter in the given string and then split it into tokens when the delimiter occurs. And the substr() function stores the sub-string to be printed. On the other hand, an erase() function stores the current position of the string and moves to the next token, and this process continues until we have got all the split strings.






Latest Courses