Home » Technical Interview Questions » String Interview Questions » Split a string

Split a string

Reading Time - 1 mins

Write a program to Split a given input string by any delimiter.


a)  Input string : Tutorial-Cup
Output : Tutorial

b) Input string: C, C++, Java
Output: C

Time complexity : O(n)


Split string[] according to the given delimiters,

1. Traverse input_string.

2. Keep printing tokens while any delimiters present.

3. Return Null, when there are no more tokens.

C++ Program

#include <bits/stdc++.h>

using namespace std;
//Function to split a string 
int main()
    char str[] = "Tutorial-Cup";
    //Traverse string and print token 
    //till we find delimiter
    char *token = strtok(str, "-");
    while (token != NULL)
        token = strtok(NULL, "-");
    return 0;

Try It


READ  Reorganize String
Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions