Implement substr function in C

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

 
 

The prototype of substr function 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

 

Below’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).
 

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz