Split the string into substrings using delimiter | C ++ implementation to split string into substrings on the basis of delimiter ; function to split string into substrings on the basis of delimiter and return the substrings after split ; to count the number of split strings ; adding delimiter character at the end of ' str ' ; length of ' str ' ; traversing ' str ' from left to right ; if str [ i ] is not equal to the delimiter character then accumulate it to ' word ' ; if ' word ' is not an empty string , then add this ' word ' to the array ' substr _ list [ ] ' ; reset ' word ' ; return the splitted strings ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > splitStrings ( string str , char dl ) { string word = " " ; int num = 0 ; str = str + dl ; int l = str . size ( ) ; vector < string > substr_list ; for ( int i = 0 ; i < l ; i ++ ) { if ( str [ i ] != dl ) word = word + str [ i ] ; else { if ( ( int ) word . size ( ) != 0 ) substr_list . push_back ( word ) ; word = " " ; } } return substr_list ; } int main ( ) { string str = " geeks ; for ; geeks " ; char dl = ' ; ' ; vector < string > res = splitStrings ( str , dl ) ; for ( auto x : res ) cout << x << endl ; return 0 ; }