Pre-defined Library Functions in C

In this Era of Programming, having knowledge of writing our own code for a given problem is must, but we should also be aware of the pre-defined functions that are provided to us in different libraries.


In Strings, several pre-defined functions are there in the <string.h> header file or library, which are useful in certain operations on strings. Some of the important and most used functions are listed below :

Function

Use

strlen()

calculates the length of the string

strcat()

Appends one string at the end of another

strncat()

Appends first n-characters of a string at the end of another string

strcpy()

Copies a string into another

strncpy()

Copies first n-characters of one string into another

strcmp()

Compare two strings

strncmp()

Compare first n-characters of two strings

strchr()

Helps in finding the 1st occurrence of a given character in a string

strrchr()

Helps in finding the last occurrence of a given character in a string

strstr()

Finds the first occurrence of a given string in another string

strrev()

Finds the reverse of the string

strtok()

splits a string into token


**********************

1. strlen() : This function is used to calculate the length of the string, it returns the length of the string as output . The returned value is of unsigned integer type. It doesn't counts the NULL character in the length.

#include <stdio.h>
#include <string.h>
int main()
{   int l1,l2;
    char string1[50]="Coder";
    char string2[50]={'c','o','d','e','r','\0'};
    l1=strlen(string1);
    l2=strlen(string2);
    
    printf("Length of string1 is %d\n",l1);
    printf("Length of string2 is %d\n",l2);

    return 0;
}

OUTPUT

Length of string1 is 5
Length of string2 is 5

**********************

2. strcat() : This function is used to join (concatenate) 2 strings. It joins 1 string to the end of other. After joining, the new string formed is stored in the string whose name is written first in the function, and its size should also be much larger so as to accomodate the new string, otherwise an error can occur(Segmentation Fault).

#include <stdio.h>
#include <string.h>
int main() {
   char str1[100] = "Hello";
   char str2[10] = " World";
   strcat(str1, str2);
   
   puts(str1);
   puts(str2);

   return 0;
}

OUTPUT 

Hello World
 World

**********************

3. strncat() : This function helps in joining 2 strings but only for a specific number of characters. It is also called Bounded String Concatenation. It returns pointer of the destination string to whom the strings are joined. 

#include <stdio.h>
#include <string.h>
int main () 
{
   char str1[100] = "Hello";
   char str2[10] = "World";

   //After Concatenation
   printf("String formed would be %s\n", strncat(str1, str2, 2));

   // printing return value of strncat()
   printf("Destination String is str1: %s", str1);

   return 0;
}

OUTPUT

String after concatenation would be HelloWo
Destination String is str1: HelloWo

**********************

4. strcpy() : This fucntion copies the 1 string to another, i.e source to destination. If the size of the string we are copying the text is small, then un-defined behaviour can occur. Like Buffer Overflow.

#include <stdio.h>
#include <string.h>
int main()
{
   char str1[100] = "Programmer Studios";
   char str2[100];
   strcpy(str2, str1);

   puts(str2);
   return 0;
}

OUTPUT

Programmer Studios

**********************

5. strncpy() : This function copies a string upto first n-characters.

#include <stdio.h>
#include <string.h>
int main() 
{
   char str1[20] = "Programmer Studios";
   char str2[20];
   strcpy(str2, str1,10);

   puts(str2);
   return 0;
}

OUTPUT

Programmer

**********************

6. strcmp() : This functions compares 2 strings and returns 0 is both are equal, or return a negative integer if the ASCII value of the 1st un-matched integer is smaller than the second, or returns a positive integer if the ASCII value of the 1st un-matched integer is greater than the second. It compares the strings character by character and continues until NULL character is not encountered.

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[10] = "abcd";
    char str2[10] = "abed";
    char str3[10] = "Lbem";
    int r;

    r = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", r);

    r = strcmp(str1, str3);
    printf("strcmp(str1, str3) = %d\n", r);

    return 0;
}

OUTPUT

strcmp(str1, str2) = -1
strcmp(str1, str3) = 1

**********************

7. strncmp() : It compares first n-characters of the strings only. And then return the integer value correspondingly.

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[10] = "abcd";
    char str2[10] = "abERkl";
    int r;

    r = strncmp(str1, str2, 4);
    printf("strncmp(str1, str2,4) = %d\n", r);

    return 0;
}

OUTPUT

strncmp(str1, str2) = 1

**********************

8. strchr() : This function searches for the 1st occurence of a character(suppose a), in the string. Then returns a pointer to the 1st occurence of that character, or NULL if it is not found.

#include <stdio.h>
#include <string.h>
int main ()
{
   char str[] = "Programmer Studios";
   char ch = 'm';
   char *r;

   r = strchr(str, ch);

   printf("String is %s\n", r);
   
   return(0);
}

OUTPUT

mmer Studios

**********************

9. strrchr() : This function searches for the last occurence of a character(suppose a), in the string. Then returns a pointer to the last occurence of that character, or NULL pointer if it is not found.

#include <stdio.h>
#include <string.h>
int main ()
{
   char str[] = "Programmer Studios";
   char ch = 'r';
   char *r;

   r = strrchr(str, ch);

   printf("String is %s\n", r);
   
   return(0);
}

OUTPUT

r Studios

**********************

10. strstr() : This function helps in finding the 1st occurence of a sub string in our string. It returns a pointer to the 1st occurence of the sub-string or returns null pointer if there is no sub string.

#include <stdio.h>
#include <string.h>
int main () {
   char str[20] = "Programmer Studios";
   char sub[10] = "Studios";
   char *r;

   r = strstr(str,sub);

   printf("The substring is: %s\n", r);
   
   return(0);
}

OUTPUT

Studios

**********************

11. strrev() : This function is used to reverse the given string.

#include<stdio.h>
#include<string.h>
int main()
{
   char str[30] = "Coder";
 
   printf("String before reversing is %s\n",str);
 
   printf("String after reversing is %s",strrev(str));
 
   return 0;
}

OUTPUT

redoC

**********************

12. strtok() : This function tokenizes/parses a given string using a delimiter. It returns a pointer to the 1st token found otherwise returns null pointer is token is not found in the whole string.

#include <string.h>
#include <stdio.h>
int main () 
{
   char str[80] = "Coding,Programming,Writing";
   char t[2] = ",";
   char *token;

   token = strtok(str, t);

   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, t);
   }
   
   return(0);
}

OUTPUT

Coding
Programming
Writing



Ritish

Just a novice blogger

1 Comments

  1. I am not a programmer but I find your blog so simple and informative for me to easily understand the codes. Keep sharing helpful articles.

    ReplyDelete
Post a Comment
Previous Post Next Post