[Snippets] String Functions in C

This is a discussion on [Snippets] String Functions in C within the C Programming forums, part of the General Programming Boards category; Some usable string functions. Code: #include <stdio.h> #include <stdlib.h> /* String Length */ int str_len(char *string) { char *count = ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Thumbs up [Snippets] String Functions in C

    Some usable string functions.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* String Length */
    int str_len(char *string)
    {
        char *count = string;
         while(*count) {count++;}
        return count - string;
    }
    
    
    /* String Append */
    char* str_append(char* string, char* append) {
        char* newstring = NULL;
        size_t needed = snprintf(NULL, 0, "%s%s", string, append);
        newstring = malloc(needed);
        sprintf(newstring, "%s%s", string, append);
        return newstring;
    }
    
    
    /* String Append Char */
    char* str_append_chr(char* string, char append) {
        char* newstring = NULL;
        size_t needed = snprintf(NULL, 0, "%s%c", string, append);
        newstring = malloc(needed);
        sprintf(newstring, "%s%c", string, append);
        return newstring;
    }
    
    
    /* String Equals */
    int str_equals(char *equal1, char *eqaul2)
    {
       while(*equal1==*eqaul2)
       {
          if ( *equal1 == '\0' || *eqaul2 == '\0' ){break;}
          equal1++;
          eqaul2++;
       }
       if(*eqaul1 == '\0' && *eqaul2 == '\0' ){return 0;}
       else {return -1};
    }
    
    
    /* String Replace */
    char* str_replace(char* search, char* replace, char* subject) {
        char* newstring = "";
        int i = 0;
        for(i = 0; i < str_len(subject); i++) {
            if (subject[i] == search[0]) {
                int e = 0;
                char* calc = "";
                for(e = 0; e < str_len(search); e++) {
                    if(subject[i+e] == search[e]) {
                        calc = str_append_chr(calc, search[e]);
                    }
                }
                if (str_equals(search, calc) == 0) {
                    newstring = str_append(newstring, replace);
                    i = i + str_len (search)-1;
                }
                else {
                    newstring = str_append_chr(newstring, subject[i]);
                }
            }
            else {
                newstring = str_append_chr(newstring, subject[i]);
            }
        }
        return newstring;
    }
    
    
    /* String Replace Maximal */
    char* str_replace_max(char* search, char* replace, char* subject, int count) {
        char* newstring = "";
        int i = 0;
        for(i = 0; i < str_len(subject); i++) {
            if (subject[i] == search[0]) {
                int e = 0;
                char* calc = "";
                for(e = 0; e < str_len(search); e++) {
                    if(subject[i+e] == search[e]) {
                        calc = str_append_chr(calc, search[e]);
                    }
                }
                if (str_equals(search, calc) == 0) {
                    if(count > 0) {
                        newstring = str_append(newstring, replace);
                        i = i + str_len (search)-1;
                        count = count - 1;
                    }
                    else {
                        newstring = str_append_chr(newstring, subject[i]);
                    }
                    
                }
                else {
                    newstring = str_append_chr(newstring, subject[i]);
                }
            }
            else {
                newstring = str_append_chr(newstring, subject[i]);
            }
        }
        return newstring;
    }
    

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,438
    Usable? I disagree.

    You are aware C standard library provides functions, specified in the <string> header, that provide capabilities similar to - and possibly more general - than your snippets?

    Your str_append_chr() function dynamically allocates memory, and some of your other functions call it in a loop without ever releasing that dynamically allocated memory. That is a SEVERE memory leak, which would discourage anyone using your functions for serious work. C is not garbage collected.

    Using snprintf() and sprintf() to copy strings around is among the least efficient techniques you could have used. While I actively discourage things like premature optimisation, code that uses premature inefficiency is not what I recommend.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Hell, it wouldn't even compile.

    Code:
    /* String Equals */
    int str_equals(char *equal1, char *eqaul2)
    {
       while(*equal1==*eqaul2)
       {
          if ( *equal1 == '\0' || *eqaul2 == '\0' ){break;}
          equal1++;
          eqaul2++;
       }
       if(*eqaul1 == '\0' && *eqaul2 == '\0' ){return 0;}
       else {return -1};
    }
    Fail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Snippets?
    By Akkernight in forum General Discussions
    Replies: 24
    Last Post: 07-11-2009, 06:51 AM
  2. Storing Code Snippets
    By saeculum in forum Linux Programming
    Replies: 1
    Last Post: 03-15-2009, 08:47 PM
  3. Joining these code snippets
    By spadez in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 03:07 PM
  4. my C snippets
    By manav-II in forum C Programming
    Replies: 20
    Last Post: 09-19-2008, 06:52 AM
  5. C/C++ Snippets
    By dune911 in forum C Programming
    Replies: 0
    Last Post: 10-09-2002, 01:05 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21