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

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of