fork download
  1. #include <stdio.h>
  2. #include <stdlib.h> // malloc
  3.  
  4. char* substr( const char* str, size_t index, size_t length )
  5. {
  6. int i;
  7. char* temp = (char*)malloc( length + 1 );
  8. const char* s = str + index;
  9. for( i = 0; i < length && s[ i ]; ++i )
  10. temp[ i ] = s[ i ];
  11. for( ; i < length; ++i ) temp[ i ] = ' ';
  12. temp[ length ] = 0;
  13. return temp;
  14. }
  15.  
  16. int main()
  17. {
  18. char* str;
  19. puts( str = substr( "hello world", 6, 3 ) );
  20. free( str );
  21.  
  22. puts( str = substr( "hello world", 6, 10 ) );
  23. free( str );
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
wor
world