Advertisement
Guest User

Untitled

a guest
Sep 12th, 2015
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. /*
  4. The strdup() function returns a pointer to a new string which is a duplicate of the string s.
  5. Memory for the new string is obtained with malloc(3), and can be freed with free(3).
  6. */
  7. char *strdup(s)
  8. char *s;
  9. {
  10.     char *p = malloc(strlen(s) + 1);
  11.     if (p) {
  12.       strcpy(p, s);
  13.     }
  14.     return p;
  15. }
  16.  
  17. /*
  18. Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
  19. */
  20. char *strstr(searchee, lookfor)
  21. char *searchee;
  22. char *lookfor;
  23. {
  24.   /* Less code size, but quadratic performance in the worst case.  */
  25.   if (*searchee == 0) {
  26.     if (*lookfor) {
  27.         return (char *) NULL;
  28.     }
  29.     return (char *) searchee;
  30.   }
  31.  
  32.   while (*searchee) {
  33.     int i;
  34.     i = 0;
  35.  
  36.     while (1) {
  37.       if (lookfor[i] == 0) {
  38.         return (char *) searchee;
  39.       }
  40.  
  41.       if (lookfor[i] != searchee[i]) {
  42.         break;
  43.       }
  44.       i++;
  45.     }
  46.     searchee++;
  47.   }
  48.   return (char *) NULL;
  49. }
  50.  
  51. /*
  52. Replace substr in string with replacement
  53. */
  54. char *str_replace (string, substr, replacement)
  55. char *string;
  56. char *substr;
  57. char *replacement;
  58. {
  59.   char *tok = NULL;
  60.   char *newstr = NULL;
  61.   char *oldstr = NULL;
  62.   /* if either substr or replacement is NULL, duplicate string a let caller handle it */
  63.   if ( substr == NULL || replacement == NULL ) return strdup (string);
  64.   newstr = strdup(string);
  65.   while ( (tok = strstr(newstr, substr))){
  66.     oldstr = newstr;
  67.     newstr = malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);
  68.     /*failed to alloc mem, free old string and return NULL */
  69.     if (newstr == NULL ){
  70.       free (oldstr);
  71.       return NULL;
  72.     }
  73.     memcpy(newstr, oldstr, tok - oldstr);
  74.     memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));
  75.     memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), strlen(oldstr) - strlen(substr) - (tok - oldstr));
  76.     memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement) , 0, 1);
  77.     free(oldstr);
  78.   }
  79.   return newstr;
  80. }
  81.  
  82. /*
  83. Wrap a string at x columns
  84. */
  85. VOID wrap(out, str, columns)
  86. char *out;
  87. char *str;
  88. int columns;
  89. {
  90.     int len, n, w;
  91.     int wordlen=0;
  92.     int linepos=0;
  93.     int outlen=0;
  94.     char *word;
  95.  
  96.     len = strlen(str);
  97.     for( n=0; n<=len; n++ )
  98.     {
  99.         if( str[n] == ' ' || str[n] == '\n' || n == len )
  100.         {
  101.             if( linepos > columns )
  102.             {
  103.                 out[outlen++] = '\n';
  104.                 linepos = wordlen;
  105.             }
  106.  
  107.             for( w=0; w<wordlen; w++ )
  108.             {
  109.                 out[outlen++] = word[w];
  110.                 word[w] = '\0';
  111.             }
  112.  
  113.             if( n == len ) {
  114.                 out[outlen] = '\0';
  115.             } else if(str[n] == '\n') {
  116.                 out[outlen] = str[n];
  117.                 linepos=0;
  118.             } else {
  119.                 out[outlen] = ' ';
  120.                 linepos++;
  121.             }
  122.             outlen++;
  123.             wordlen=0;
  124.         } else {
  125.             word[wordlen++] = str[n];
  126.             linepos++;
  127.         }
  128.     }
  129. }
  130.  
  131. main()
  132. {
  133.   char *testStr;
  134.   char *outStr;
  135.  
  136.   testStr = "Let's see if we can create some new UU functions";
  137.   printf("%s\n", str_replace(testStr, "UU", "string"));
  138.   /*
  139.   testStr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse blandit fermentum";
  140.   wrap(outStr, testStr, 10);
  141.   printf("%s", outStr);
  142.   */
  143.  
  144. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement