Friday, September 25, 2020

Explain string functions with example.

 


1] gets() :

▪️ The function gets() receives the string from an input device. 

▪️ At the end string, the function gets() appends a null character.

▪️ The gets() function takes the following from:

                    gets(str);

▪️ Example:

void main()

{

   char str[80];

    printf( "Enter your name ");

    gets(str);

    printf(" Welcome !  %s",str);

}

Output:

    Enter your name

       Sky Eagle

     Welcome ! Sky Eagle

2] strlen() function:

▪️ This function returns length of string passing to it.

3 Hence it returns an integer value that must be collected within a variable .

4 It takes the following gereral format.      

                 L = strlen ( str );

Example:

#include<stdio.h>

#include<string.h>

Void main()

{

       char str[ ]="Hello friends";

       int length;

       length=strlen(str);

       Printf("length of string=%d",length);

}

Output:

      length of string = 6

3] strcpy() function:

▪️ This function copies one string in to another string variable. 

▪️ the base address of source string and target string should be supplied to function.

▪️ The source string remains as it is , while target string will be changed.

▪️ The general form of strcpy() is

     L = strcpy(targetstring, sourcestring);

▪️ Example:

#include<string.h>

void main()

{

    char target[10]="Bad",source[10]="Good";

     strcpy(target,source);

     printf("target string=%s",target);

}

Output:

target string = Good

4] strncpy() function:

▪️ This function copies starting  characters from the source string to target string .

▪️ The base address of source string and target string should be supplied to function.

▪️ The general form of strncpy() is

    Strncpy(target string, source string ,N)

Example:

#include<stdio.h>

#include<string.h>

void main()

{

    char target [20],source[20]="Happy Birthday";

strncpy(target,source,5);

printf("target string=%s",target);

}

Output:

target string = Happy





1 comment:

  1. Informative but use smartphone friendly themes
    Ckeck mine
    https://vcarskill2020.blogspot.com/2020/09/kia-sonet-review.html?m=1
    https://vcarskill2020.blogspot.com/2020/09/toyota-urban-cruiser.html?m=1

    ReplyDelete

Object Paradigm Oriented Programming in C++ / object oriented programming / History of C++/ paradiagram in c++/C devolop, Operating System, Unix operating system, Assembly language, popular in world, Programming language.

  Object Paradigm Oriented Programming  Two programmers, Brian Kernighan and Dennis Ritchie developed a new language called C, in 1972.  C l...