String Function in C: Part-1

String Function were used to do some operations on the Strings like finding the length of the string, comparing two strings, reversing the given string, etc.

[NOTE: String function can only be used after include the header file "string.h" ]

LOGO


Why String Function ?
We use string function to do operations only by writing single line command instead of writing a bulk of code to do the same work. It will save time and effort and also reduces the chances of the error in the whole program.


Standard String Functions 
  • strlen( )
    This function is used to find out the length of any string. It contains two arguments.

    Syntax: strlen(String_name);

    Example: char str[10]; //taking a string
                    int a; //variable to store the value of length
                    gets(str); //asking for the value of the string
                    a=strlen(str); //here whatever the length is, it will store in 'a'
  • strrev( )
    This function is used to reverse the given string. It contains two arguments.

    Syntax: strrev(String_name);

    Example: char str[10]; //taking a string
                     gets(str); //variable to store the value of length
                     str=strrev(str); //here the reversed string will store in the same variable "str"
  • strcpy( )
    This function is used to copy one string to an another string. It contains two arguments.

    Syntax: strcpy(Destination_String, Source_String);
    //here Source String refers to the string from which you want to copy the value
    //here Destination String refers to the string in which you want to copy the value


    Example: char str1[10], str2[10];
                     gets(str1);
                     strcpy(str2, str1);
  • strncpy( )
    This function is similar to "strcpy( )" but in this function we can restrict the range in which the value is copied from one string to an another string. It contains three arguments.

    Syntax: strcpy(Destination_String, Source_String, N);
    //here Source String refers to the string from which you want to copy the value
    //here Destination String refers to the string in which you want to copy the value

    //here 'N' refers to number of character you want to copy

    Example: char str1[10], str2[10];
                     gets(str1);
                     strcpy(str2, str1, 2);//here only two characters will be copied from first string
  • strcmp( )
    This function is used to compare the two strings i.e, this function will tell whether a string is similar or greater or lesser then the another string. It contains two arguments.

    Syntax: strcmp(First_String, Second_String);
    //here first string and second string are two strings to compare
    If ⤇
    1. First String is greater than Second String then the function will return positive value.
    2. First String is lesser than Second String then the function will return negative value.
    3. First String and Second String are same then the function will return the value "0".

    Example: char str1[10], str2[10]; //taking a string
                    int a; //variable to store the value
                    gets(str1); //asking for the value of the string                gets(str2);
                    a=strcmp(str1, str2); //here as per given string, a value will be stored in 'a'
                    //value which is stored can either be Positive, Negative or Zero.
  • stricmp( )
    This function is used to compare the two string by ignoring case sensitivity i.e, their will be non difference between the lower case and upper case. It contains two arguments.

    Syntax: stricmp(First_String, Second_String);
    //here first string and second string are two strings to compare without considering their case sensitivity
    If ⤇
    1. First String is greater than Second String then the function will return positive value.
    2. First String is lesser than Second String then the function will return negative value.
    3. First String and Second String are same then the function will return the value "0".

    Example: char str1[10], str2[10]; //taking a string
                    int a; //variable to store the value
                    gets(str1); //asking for the value of the string              gets(str2);
                    a=stricmp(str1, str2); //here as per given string, a value will be stored in 'a'
                    //value which is stored can either be Positive, Negative or Zero.
  • strncmp( )
    This function is used to compare strings within a finite range. It contains three arguments.

    Syntax: stricmp(First_String, Second_String, N);
    //here first string and second string are two strings to compare
    //here 'N' refers to number of character you want to compareIf ⤇
    1. First String is greater than Second String then the function will return positive value.
    2. First String is lesser than Second String then the function will return negative value.
    3. First String and Second String are same then the function will return the value "0".

    Example: char str1[10], str2[10]; //taking a string
                    int a; //variable to store the value
                    gets(str1); //asking for the values of the string                gets(str2);
                    a=strncmp(str1, str2, 2); //here as per given string, two characters will be compared
                    //value which is stored can either be Positive, Negative or Zero.
  • strcat( )
    This function is used for concatenation i.e, it will joint two different string to a single string. It contains two arguments.
    For example- PRO + CODER ➡️   PROCODER

    Syntax: strcat(First_String, Second_String);

    Example: char str1[10], str2[10];
                     gets(str1);
                     gets(str2);
                     strcpy(str2, str1, 2);//here two characters will be copied from str1
  • strupr( )
    This function is used to change the lower case string to upper case. 
    It contains two arguments.

    Syntax: strupr(String_name);

    Example: char str[10];
                     gets(str); //asking for the value of the string
                     strupr(str);
  • strlwr( )
    This function is used to change the upper case string to lower case. It contains two arguments.

    Syntax: strlwr(String_name);

    Example:
     char str[10];
                     gets(str); //asking for the value of the string
                     strlwr(str);

So these are the some Standard String Inbuilt Function.


For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments