Write an efficient function to implement substr() function in C. The substr() function returns the substring of a given string between two given indices.

The substr function prototype is: char* substr(const char *source, int m, int n)

It returns the substring of the source string starting at the position m and ending at position n-1.

Download  Run Code

Output:

funct

 
Following’s another implementation that uses C library’s strncpy() function:

Download  Run Code

Output:

funct

 
The time complexity of above functions is O(n – m).

That’s all about substr() implementation in C.