Book Your slot
X
ONLINE BOOKING
BOOK NOW
OFFLINE BOOKING
Call or WhatsApp 7993732682 (WhatsApp Now), 9177341827 (WhatsApp Now)
search
Menu Login home
  • Questions

  • Library

  • University Updates

  • Informatives

  • Technology Lines

  • Training & Internships

  • X
    Menu
  • Home
  • Privacy Policy
  • Legal Disclaimer
  • Terms & Conditions
  • Return Policy
  • About Us
  • Need any help?? write to us at

    support@engineershub.co

    Follow Us

    X
    LOGIN
    Login to access posts, links, updates, question papers, materials, one liners!
    Use Your Email Address/Mobile and Password to Login
    Forgot Password?
    Not a member? Sign Up
    LOGIN WITH EMAIL/MOBILE
    Forgot Password?
    Go Back
    FORGOT PASSWORD
    Go Back
    RESET PASSWORD
    Go Back
    Continue with LinkedIn
    OR
    Fill Up a Simple Form
    Already a Member? Login
    SIGN UP
    Fill all the below details correctly and click on Next
    Go Back
    Explain about the following string handling functions with example programs. (i) strlen (ii) strcpy (iii) strcmp (iv) strcat? - EngineersHub
    Go Back
    Question
    Anvesh Kanchibhotla
    3 years ago
    1 Answer(s) posted Write an answer 9312
    Answer
    Read Mode
    Answer posted by Deepa
    3 years ago

    C supports a number of string handling functions. All of these built-in functions are aimed at performing various operations on strings are in the header file string.in

    (i) strlen( )

    This function is used to find the length of the string excluding the NULL character. In other words, this function is used to cout the number of character in a string. Its syntax is as follows:

                                                                            Int strlen(string);

     Example: char str1[ ]="WELLCOME";

                       int n;

                       n =strlen(str1);

    /* A program to caculate length of string by using stren() function */

    #include

    main()

    {

    char string1[50];

    int length;

    printf("\n Enter any string:");

    gets(string1);

    length=strlen(string1);

    printf("\n The length of string=%d",length);

    }

    (ii) strcpy( )

    This function is used to copy one string to  the other. Its syntax is as follows:

                                                                     strcpy(strings1,strings2);

    Where string1 and string2 are one -dimmensional character arrays.

     This function copies the content of strings2 to string1.

                           E.g.,string1 contains master and string2 contains madam, then string1 holds madam after the execution of strcpy(string1,string2) function.

    Example :         char str1[ ] ="WELLCOME";

                  char str2[ ]="HELLO";

                  strcpy(str1,str2);

    /* A program to copy one string to another using strcpy( ) function */

        #include

        #include

        main( )

        {

        char string1[30],string2[30];

        printf("\n Enter first string:");

         gets(string1);

         printf("\n Enter second string:");

         gets(string2)

         strcpy(string1,string2);

         printf("\n first string=%s",string1);

         printf("\n second string=%s",string2);

    }

     (iii) strcmp( )

       This function compares two stings characters by character b(ASCII comparison) and returns one of three values {-1,0,1}. The numeric difference is'0' if strings is equal. If it is negative string1 is alphabetically above string2. If it is positive string2 is alphabetically above string1.

                                 Its syntax is as follows:

                                                                                            int strcmp(string1,string2);

    Example:     char str1[ ]="ROM";

               char str2[ ]="RAM";

               strcmp(str1,str2);

                     (or)

               strcmo("ROM","RAM");

    /* a program to compare two strings using strcmp() function */

    #incudestdio.h>

    #include

    main()

    {

    char string1[30],string2[15];

    int x;

    prinf(""\n Enter first string:");

    gets(string1);

    printf("\n Enter seond string:");

    gets(strings2);

    x= strcmp(string1,string2);

    if(x==0)

    printf("\nBoth strings are equal");

    else if(x>0)

    printf("\n first string is bigger");

    else

    printf("\n second  string is bigger");

    }

    (iv) strcat ( )

    This fuction is used to concatenate two strings. i.e.,it appends one string at the end of the specified string.Its syntax as follows:

                                                                    strcat(string1,string2);

    where string1 and string2 are one-dimensional character arrays.

                                            This function joins two strings together. In other words, it adds the string2 to string1 and the strings1 contains the concatenated string. E.g.,string1 contains prog and string2 contains ram , then string1 holds program after the execution of the strcat( ) function. 

                                Example:    char str1[10] = "VERY";

                            char str2[5] = "GOOD";

                            strcat(str1,str2);

    /* A program to concatenate one string with another using stracat( ) function */

    #include

    #include

    main( )

    char string1[30],string2[15];

    printf("\n Enter first string:");

    gets(string1);

    printf("\n Enter second string:");

    gets(strings2);

    strcat(string1,string2);

    printf("\n concatenated string=%s",string1);

    }

    X
    Explain about the following string handling functions with example programs. (i) strlen (ii) strcpy (iii) strcmp (iv) strcat?
    X
    Explain about the following string handling functions with example programs. (i) strlen (ii) strcpy (iii) strcmp (iv) strcat?

    C supports a number of string handling functions. All of these built-in functions are aimed at performing various operations on strings are in the header file string.in

    (i) strlen( )

    This function is used to find the length of the string excluding the NULL character. In other words, this function is used to cout the number of character in a string. Its syntax is as follows:

                                                                            Int strlen(string);

     Example: char str1[ ]="WELLCOME";

                       int n;

                       n =strlen(str1);

    /* A program to caculate length of string by using stren() function */

    #include

    main()

    {

    char string1[50];

    int length;

    printf("\n Enter any string:");

    gets(string1);

    length=strlen(string1);

    printf("\n The length of string=%d",length);

    }

    (ii) strcpy( )

    This function is used to copy one string to  the other. Its syntax is as follows:

                                                                     strcpy(strings1,strings2);

    Where string1 and string2 are one -dimmensional character arrays.

     This function copies the content of strings2 to string1.

                           E.g.,string1 contains master and string2 contains madam, then string1 holds madam after the execution of strcpy(string1,string2) function.

    Example :         char str1[ ] ="WELLCOME";

                  char str2[ ]="HELLO";

                  strcpy(str1,str2);

    /* A program to copy one string to another using strcpy( ) function */

        #include

        #include

        main( )

        {

        char string1[30],string2[30];

        printf("\n Enter first string:");

         gets(string1);

         printf("\n Enter second string:");

         gets(string2)

         strcpy(string1,string2);

         printf("\n first string=%s",string1);

         printf("\n second string=%s",string2);

    }

     (iii) strcmp( )

       This function compares two stings characters by character b(ASCII comparison) and returns one of three values {-1,0,1}. The numeric difference is'0' if strings is equal. If it is negative string1 is alphabetically above string2. If it is positive string2 is alphabetically above string1.

                                 Its syntax is as follows:

                                                                                            int strcmp(string1,string2);

    Example:     char str1[ ]="ROM";

               char str2[ ]="RAM";

               strcmp(str1,str2);

                     (or)

               strcmo("ROM","RAM");

    /* a program to compare two strings using strcmp() function */

    #incudestdio.h>

    #include

    main()

    {

    char string1[30],string2[15];

    int x;

    prinf(""\n Enter first string:");

    gets(string1);

    printf("\n Enter seond string:");

    gets(strings2);

    x= strcmp(string1,string2);

    if(x==0)

    printf("\nBoth strings are equal");

    else if(x>0)

    printf("\n first string is bigger");

    else

    printf("\n second  string is bigger");

    }

    (iv) strcat ( )

    This fuction is used to concatenate two strings. i.e.,it appends one string at the end of the specified string.Its syntax as follows:

                                                                    strcat(string1,string2);

    where string1 and string2 are one-dimensional character arrays.

                                            This function joins two strings together. In other words, it adds the string2 to string1 and the strings1 contains the concatenated string. E.g.,string1 contains prog and string2 contains ram , then string1 holds program after the execution of the strcat( ) function. 

                                Example:    char str1[10] = "VERY";

                            char str2[5] = "GOOD";

                            strcat(str1,str2);

    /* A program to concatenate one string with another using stracat( ) function */

    #include

    #include

    main( )

    char string1[30],string2[15];

    printf("\n Enter first string:");

    gets(string1);

    printf("\n Enter second string:");

    gets(strings2);

    strcat(string1,string2);

    printf("\n concatenated string=%s",string1);

    }

    EngineersHub Logo
    x
    Loading...