How to Split a String Based on Empty/Blank Lines in C++? Last Updated : 19 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, strings are objects that represent a sequence of characters and we can break this string into a new line using the '\n' escape sequence. In this article, we will learn how to split a string by empty or blank lines in C++. Example: Input: string inputString= "Hello\n\nWorld\n\nThis\n\nis\n\nGeeksForGeeks"; Output: Hello World This is GeeksForGeeksSplit a String into Words by Empty Lines in C++To split a std::string by empty lines, we can use the stringstream with a combination of std::getline function having ā\nā as the delimiter. We can keep taking input using getline() till there is no string left. ApproachCreate a stringstream to the given string.Create a string variable to store the splitter strings.Run a loop using std::getline with ā\nā as the delimiter.If the line is not empty, add it to the result.Repeat the process until the end of the string is reached.C++ Program to Split a String Based on Empty/Blank LinesThe below program illustrates how we can split a string based on empty/blank lines in C++. C++ // C++ Program to illustrate how to split a string by empty // or blank lines #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; // Function to split a string by empty lines vector<string> splitStringByEmptyLines(string& inputString) { vector<string> result; stringstream ss(inputString); string line; // Loop until the end of the string while (getline(ss, line)) { if (!line.empty()) { result.push_back(line); } } return result; } int main() { // string to be splitted string inputString = "Hello\n\nWorld\n\nThis\n\nis\n\nGeeksForGeeks"; // calling the function to split the string by empty // lines vector<string> substrings = splitStringByEmptyLines(inputString); // Displaying the string after splitting for (const auto& substring : substrings) { cout << substring << endl; } return 0; } OutputHello World This is GeeksForGeeks Time Complexity: O(N) Auxiliary Space: O(N) , here N is the length of the original string. Comment More infoAdvertise with us Next Article How to Split a String by a Delimiter in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-strings CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read How to Split a String by a Delimiter in C++? Splitting a string is the process of dividing the given string into multiple substrings on the basis of a character (or substring) as the separator. This separator is called delimiter and the whole process is also called tokenization.ExamplesInput: str = "geeks,for,geeks", delimiter = (,)Output: gee 4 min read How to Check if a String is Empty in C++? In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a 2 min read How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee 2 min read How to Create a Stack of Strings in C++? In C++, Stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read Like