Breaking

Friday, February 7, 2020

What is Library in the programming world?
It is the same as our study library. But here we store functions instead of books. You can simply develop your own library by adding more than one function to it. All you just need to do is, write a method, and keep it inside your library. whenever you want to use that method in your program, you can include that library as a file header and just make a call to that method. definition of that method is inside of your library. So no need to write a bunch of code for the same method. As many times you can call the method and can get the return value without defining it in your program.

There are two types of libraries. Static library and Dynamic library. We can discuss it in a later post. This is for some important methods used in string.h library.


There are several library functions to manipulate strings and the prototypes for these functions are in header file string.h. In this Page we will discuss how to use some of the most commonly used string functions like strlen(), strcpy(), strcmp(), strcat(). and see how these functions are created.



strcat:-

Synopsis: 
       char *strcat(char *dest, const char *src);

Description:
The strcat()  function appends the src string to the dest string, overwriting the terminating null byte  ('\0')  at the end of dest,  and then adds a terminating null byte.  The strings may not overlap, and the dest string must have enough space for the result.  If dest is not large enough, program behavior is unpredictable;  buffer overruns are a  favorite avenue for attacking secure programs.

Implementation:

char* strcat(char* destination, const char* source){


    char* ptr = destination ;

    while(*destination++)
    ptr++;
    while(*ptr++ = *source++);
    return destination;
}
strcmp:-

Synopsis:
       int strcmp(const char *s1, const char *s2);

Description:
The strcmp() function compares the two strings s1 and s2.  It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

Implementation:


int strcmp(const char *l, const char *r){


    while ( *l++==*r++ && *l);

    return *(unsigned char *)l - *(unsigned char *)r;
}

strcpy:-

Synopsis:
       char *strcpy(char *dest, const char *src);

Description:
The strcpy()  function copies the string pointed to by src, including the terminating null byte  ('\0'),  to the buffer pointed to by dest.  The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns! 

Implementation:

char* strcpy(char* destination, const char* source){


    char *ptr = destination;

    while (*destination++ = *source++);
    return ptr;
}

strlen:-

Synopsis:
       size_t strlen(const char *s);

Description:
The strlen()  function calculates the length of the string s, excluding the terminating null byte ('\0').

Return value:
The strlen() function returns the number of bytes in the string.

Implementation:

int strlen(char *str){ 


    int len=0;

    while(*str++) len++;
    return(len);
}

These are some most important library functions in string.h library. Comments for code explanation and stay tuned for more updates.
close