Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*
- The strdup() function returns a pointer to a new string which is a duplicate of the string s.
- Memory for the new string is obtained with malloc(3), and can be freed with free(3).
- */
- char *strdup(s)
- char *s;
- {
- char *p = malloc(strlen(s) + 1);
- if (p) {
- strcpy(p, s);
- }
- return p;
- }
- /*
- Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
- */
- char *strstr(searchee, lookfor)
- char *searchee;
- char *lookfor;
- {
- /* Less code size, but quadratic performance in the worst case. */
- if (*searchee == 0) {
- if (*lookfor) {
- return (char *) NULL;
- }
- return (char *) searchee;
- }
- while (*searchee) {
- int i;
- i = 0;
- while (1) {
- if (lookfor[i] == 0) {
- return (char *) searchee;
- }
- if (lookfor[i] != searchee[i]) {
- break;
- }
- i++;
- }
- searchee++;
- }
- return (char *) NULL;
- }
- /*
- Replace substr in string with replacement
- */
- char *str_replace (string, substr, replacement)
- char *string;
- char *substr;
- char *replacement;
- {
- char *tok = NULL;
- char *newstr = NULL;
- char *oldstr = NULL;
- /* if either substr or replacement is NULL, duplicate string a let caller handle it */
- if ( substr == NULL || replacement == NULL ) return strdup (string);
- newstr = strdup(string);
- while ( (tok = strstr(newstr, substr))){
- oldstr = newstr;
- newstr = malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);
- /*failed to alloc mem, free old string and return NULL */
- if (newstr == NULL ){
- free (oldstr);
- return NULL;
- }
- memcpy(newstr, oldstr, tok - oldstr);
- memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));
- memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), strlen(oldstr) - strlen(substr) - (tok - oldstr));
- memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement) , 0, 1);
- free(oldstr);
- }
- return newstr;
- }
- /*
- Wrap a string at x columns
- */
- VOID wrap(out, str, columns)
- char *out;
- char *str;
- int columns;
- {
- int len, n, w;
- int wordlen=0;
- int linepos=0;
- int outlen=0;
- char *word;
- len = strlen(str);
- for( n=0; n<=len; n++ )
- {
- if( str[n] == ' ' || str[n] == '\n' || n == len )
- {
- if( linepos > columns )
- {
- out[outlen++] = '\n';
- linepos = wordlen;
- }
- for( w=0; w<wordlen; w++ )
- {
- out[outlen++] = word[w];
- word[w] = '\0';
- }
- if( n == len ) {
- out[outlen] = '\0';
- } else if(str[n] == '\n') {
- out[outlen] = str[n];
- linepos=0;
- } else {
- out[outlen] = ' ';
- linepos++;
- }
- outlen++;
- wordlen=0;
- } else {
- word[wordlen++] = str[n];
- linepos++;
- }
- }
- }
- main()
- {
- char *testStr;
- char *outStr;
- testStr = "Let's see if we can create some new UU functions";
- printf("%s\n", str_replace(testStr, "UU", "string"));
- /*
- testStr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse blandit fermentum";
- wrap(outStr, testStr, 10);
- printf("%s", outStr);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement