SHARE
TWEET

substring()

a guest Jun 19th, 2017 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. size_t substring( char * dst, const char * src, size_t start, size_t len )
  6. {
  7.     size_t dstlen = len;
  8.     size_t srclen = strlen(src);
  9.  
  10.     if( start + len > srclen )
  11.         dstlen = srclen;
  12.  
  13.     memcpy( dst, &src[start], dstlen );
  14.  
  15.     dst[ dstlen ] = '\0';
  16.  
  17.     return dstlen;
  18. }
  19.  
  20.  
  21. int main( int argc, char * argv[] )
  22. {
  23.     char origem[] = "algoritmo";
  24.     char destino[64];
  25.  
  26.     /* Testando... */
  27.  
  28.     substring( destino, origem, 0, 4 );
  29.     printf( "substring( 0, 4 ) --> %s\n", destino );
  30.  
  31.     substring( destino, origem, 4, 5 );
  32.     printf( "substring( 5, 5 ) --> %s\n", destino );
  33.  
  34.     substring( destino, origem, 3, 3 );
  35.     printf( "substring( 3, 3 ) --> %s\n", destino );
  36.  
  37.     return 0;
  38. }
  39.  
  40. /* fim-de-arquivo */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top