Given a string and a delimiter character. Split the string based on the delimiter and print the list of resulting sub strings.
Input : str = "geeks;for;geeks" d_ch = ';' Output : geeks for geeks Input : str = "##ayush##jauhari####" d_ch = '#' Output : ayush jauhari
splitStrings(str, substr_list, dl) Initialize word = "" Initialize num = 0 str = str + dl l = str.size for i = 0 to l-1 if str[i] != dl word = word + str[i] else if word.size != 0 substr_list[num] = word num++ word = "" return num
This algorithm will fill in the splitted substrings in the array substr_list and will return the number of such substrings as num.
# Python 3 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
def splitStrings(st, dl):
word = “”
# to count the number of split strings
num = 0
# adding delimiter character at
# the end of ‘str’
st += dl
# length of ‘str’
l = len(st)
# traversing ‘str’ from left to right
substr_list = 
for i in range(l):
# if str[i] is not equal to the
# delimiter character then accumulate
# it to ‘word’
if (st[i] != dl):
word += st[i]
# if ‘word’ is not an empty string,
# then add this ‘word’ to the array
if (len(word) != 0):
# reset ‘word’
word = “”
# return the splitted strings
# Driver Code
if __name__ == ‘__main__’:
str = “geeks;for;geeks”
dl = ‘;’
res = splitStrings(str, dl)
for x in range(len(res)):
# This code is contributed by
geeks for geeks
Time Complexity: O(n), where n is the length of the given string.
- Replace two substrings (of a string) with each other
- Number of substrings of a string
- Lexicographical concatenation of all substrings of a string
- Count of substrings of a binary string containing K ones
- Program to print all substrings of a given string
- Number of even substrings in a string of digits
- Number of substrings of one string present in other
- Different substrings in a string that start and end with given strings
- Sum of all substrings of a string representing a number | Set 1
- Counting even decimal value substrings in a binary string
- Number of substrings with odd decimal value in a binary string
- Minimum changes to a string to make all substrings distinct
- Number of substrings divisible by 6 in a string of integers
- How to split a string in C/C++, Python and Java?
- Given a binary string, count number of substrings that start and end with 1.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.