C tutorial - functions and descriptions/ Explain

Programming languages or concepts


<String.h> functions and descriptions/ Explain

String.h header file in the C standard library for c programming language which contains micro definitions constant and declarations of function and types using not only for string handling but also variable memory function.
    String.h header defines one variable type, one macro & various function for manipulating array of characters


Following is a <String.h> header  function and it's description with examples:-


  1. strlen():-
          The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.
Syntax:
          size_t strlen(const char *str)

str − This is the string whose length is to be found.

Return Value
This function returns the length of string.

#include <stdio.h>
 #include <string.h>
 int main()
 { 
char str1[20] = "insproutsoftware"; printf("Length of string str1: %d", strlen(str1)); return 0; 
}

OUTPUT

Length of string str1: 16

      2.  strnlen( ):- 
                   It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.
Syntax:

size_t strnlen(const char *str, size_t maxlen)

Ex.

#include <stdio.h> 
#include <string.h>
 int main() 
char str1[20] = "insproutsoftware"; printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30)); 
printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10)); 
return 0;
 }

      3.  memchr():-
               This C library function searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str.

     Syntax:
        void *memchr(const void *str, int c, size_t n)
 
  str: is a pointer to the block memory where perform the task.
  n :- is a number of bytes to be analysed

     4.  memcmp():-
Syntax:-
             int memcmp(const void *str1, const void *str2, size_t n)

str1 − This is the pointer to a block of memory.

str2 − This is the pointer to a block of memory.

n − This is the number of bytes to be compared.

Return Value
if Return value < 0 then it indicates str1 is less than str2.

Description:-
           This is a C library function to compares the first n bytes of memory area str1 and memory area str2.

     4. memcpy():-
            This is a c library function used for copies n characters from memory area src to memory area dest.

   Syntax:
      void *memcpy(void *dest, const void * src, size_t n)

Parameters

dest − This is pointer to the destination array where the content is to be copied

src − This is pointer to the source of data to be copied, type-casted to a pointer of type void*.

n − This is the number of bytes to be copied.

Return Value
This function returns a pointer to destination, which is str1.

   5.  memset():-
            This is a C library function used for copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str.
   Syntax:-
        void *memset(void *str, int c, size_t n)

str − This is a pointer to the block of memory to fill.
n − This is the number of bytes to be set to the value.

Return Value
This function returns a pointer to the memory area str.

6. memmove():-
       This is a C library function used forcopies n characters from str2 to str1, but for overlapping memory blocks, memmove() is a safer approach than memcpy().

void *memmove(void *str1, const void *str2, size_t n)

str1 − This is a pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

str2 − This is a pointer to the source of data to be copied, type-casted to a pointer of type void*.

n − This is the number of bytes to be copied.

  8.  strcat():-
          This is C library function used for appends the string pointed to by src to the end of the string pointed to by dest.

    Syntax:
   char *strcat(char *dest, const char *src)

Parameters
dest − This is pointer to the destination array, which should contain a C string.

src − This is the string to be appended. This should not overlap the destination.

Return Value
This function returns a pointer to the resulting string dest.

  9. strncat():-
         This is a C library function used for appends the string pointed to by src to the end of the string pointed to by dest up to n characters long.

Syntax:
    char *strncat(char *dest, const char *src, size_t n)

src − This is the string to be appended.

n − This is the maximum number of characters to be appended.

Return Value
This function returns a pointer to the resulting string dest.

   10. strchr():-
             This is a C library function used for searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str.

Syntax:
      char *strchr(const char *str, int c)

Parameters
str − This is the C string to be scanned.

c − This is the character to be searched in str.

Return Value
This returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found.

    11.strcmp():-
            This is a C library function used for compares the string pointed to, by str1 to the string pointed to by str2.

    Syntax:-
     int strcmp(const char *str1, const char *str2)
Parameters
str1 − This is the first string to be compared.

str2 − This is the second string to be compared.


   12.  strncmp():-
                 This is a C library function used for compares at most the first n bytes of str1 and str2.

Syntax:-
      int strncmp(const char *str1, const char *str2, size_t n)

Parameters
str1 − This is the first string to be compared.

str2 − This is the second string to be compared.

n − The maximum number of characters to be compared.


    13.  strcoll():-
                This is a C library function used for compares string str1 to str2. The result is dependent on the LC_COLLATE setting of the location.

    Syntax:-
       int strcoll(const char *str1, const char *str2)

Parameters
str1 − This is the first string to be compared.

str2 − This is the second string to be compared.

    14.  strcpy():-
              This is a C library function used for copies the string pointed to, by src to dest.

Syntax:
      char *strcpy(char *dest, const char *src)

Parameters

dest − This is the pointer to the destination array where the content is to be copied.

src − This is the string to be copied.

Return Value
This returns a pointer to the destination string dest.

   15.  strncpy():-
              This is a C library function used for copies up to n characters from the string pointed to, by src to dest. In a case where the length of src is less than that of n, the remainder of dest will be padded with null bytes.

Syntax:
       char *strncpy(char *dest, const char *src, size_t n)

Parameters

dest − This is the pointer to the destination array where the content is to be copied.

src − This is the string to be copied.

n − The number of characters to be copied from source.

Return Value
This function returns the final copy of the copied string.

    16.  strcspn():-
                This is a C library function used for calculates the length of the initial segment of str1, which consists entirely of characters not in str2.

Syntax:
       size_t strcspn(const char *str1, const char *str2)

Parameters

str1 − This is the main C string to be scanned.

str2 − This is the string containing a list of characters to match in str1.

Return Value
This function returns the number of characters in the initial segment of string str1 which are not in the string str2.

    17.  strerror():-
                This is a C library function used for searches an internal array for the error number errnum and returns a pointer to an error message string. The error strings produced by strerror depend on the developing platform and compiler.

Syntax:
      char *strerror(int errnum)

Parameters

errnum − This is the error number, usually errno.

Return Value
This function returns a pointer to the error string describing error errnum.

   18.  strpbrk():-
              This is a C library function used for finds the first character in the string str1 that matches any character specified in str2. This does not include the terminating null-characters.

Syntax:
    char *strpbrk(const char *str1, const char *str2)

Parameters

str1 − This is the C string to be scanned.

str2 − This is the C string containing the characters to match.

Return Value
This function returns a pointer to the character in str1 that matches one of the characters in str2, or NULL if no such character is found.

    19.  strrchr():-
                  This is a C library function used for searches for the last occurrence of the character c (an unsigned char) in the string pointed to, by the argument str.

Syntax:
       char *strrchr(const char *str, int c)

Parameters

str − This is the C string.

c − This is the character to be located. It is passed as its int promotion, but it is internally converted back to char.

Return Value
This function returns a pointer to the last occurrence of character in str. If the value is not found, the function returns a null pointer.

   20.  strspn():-
                This is a C library function used for calculates the length of the initial segment of str1 which consists entirely of characters in str2.

Syntax:
     size_t strspn(const char *str1, const char *str2)

Parameters

str1 − This is the main C string to be scanned.

str2 − This is the string containing the list of characters to match in str1.

     21.  strtok():-
                 This is a C library function used for breaks string str into a series of tokens using the delimiter delim.

 Syntax:
       char *strtok(char *str, const char *delim)

Parameters

str − The contents of this string are modified and broken into smaller strings (tokens).

delim − This is the C string containing the delimiters. These may vary from one call to another.
close