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 string Input/ Output functions with example? - EngineersHub
    Go Back
    Question
    Vamshi
    3 years ago
    2 Answer(s) posted Write an answer 14928
    Answer
    Read Mode
    Answer posted by SaiMukesh Reddy
    3 years ago

    Reading and Writing strings:-

    C language provides several string handling functions for input and output.

    String Input/Output Functions:-

    C provides two basic methods to read and write strings. Using formatted input/output functions and using a special set of functions.

    Reading strings from the terminal:-

    (a) formatted input function:- scanf can be used with %s format specification to read a string.

    Ex:- char name[10];

    scanf(“%s”,name);

    Here don‘t use‘&’ because the name of the string is a pointer to the array. The problem with scanf is that it terminates its input on the first white space it finds.

    Ex:- NEW YORK

    Reads only NEW (from above example).

    (b) Unformatted input functions:-

    (1) getchar():- It is used to read a single character from the keyboard. Using this function repeatedly we may read an entire line of text

    Ex:- char ch=‘z’;

    ch=getchar();

    (2) gets():- It is a more convenient method of reading a string of text including blank spaces.

    Ex:- char line[100];

    gets(line);

    Writing strings on to the screen:-

    (1) Using formatted output functions:- printf with %s format specifier we can print strings in different formats on to screen.

    Ex:- char name[10];

    printf(“ s ”,name);

    Ex:- char name[10];

    printf(“0.4”,name);   

    J A N U

    /* If name is JANUARY prints only 4 characters ie., JANU */

    Printf(“10.4”,name);

                J A N U

    printf(“-10.4”,name);

    J A N U            

    (2) Using unformatted output functions:-

    (a) putchar():- It is used to print a character on the screen.

    Ex:- putchar(ch);

    (b) puts():- It is used to print strings including blank spaces.

    Ex:- char line[15]=“Welcome to lab”;

    puts(line);

    560
    Answer
    Read Mode
    Answer posted by Naseem Shaik
    3 years ago

    String I/O: gets() and puts()

    One of the first operations we may need for strings is the ability to read or write strings from the standard input or to the standard output.

    Library function, gets(s) reads a string from the standard input into an array pointed to by s and, puts(s) writes a string pointed to by s to the standard output.

    The prototypes for these functions declared in stdio.h, are:

    STRING gets(STRING string);

    int puts(STRING string);

    gets() function: Accepts String

    Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

    The syntax for Accepting String : gets( )

    If reading is successful, gets() returns the pointer to the string; otherwise, it returns a NULL pointer, i.e. a pointer whose value is zero. A returned value of NULL usually implies an end of the file. When gets()reads a string, it reads the input characters until a newline is read, discards the newline, appends a NULL character to the string, and stores the string where s points. 

    #include
    void main()
    {
    char name[20];
    printf("\nEnter the Name : ");
    gets(name);
    }
    

    puts() function: Printing String

    Print character string to the standard output (stdout).

    The syntax for Printing String: puts()

    Similarly, puts() outputs the string, s, after stripping the NULL and appending a newline. It returns the last character value output if successful; otherwise, it returns EOF.

    #include< stdio.h>
    #include< conio.h>
    void main()
     {
     char string[] = "This is an example string\n";
     puts(string); // String is variable Here
     puts("String"); // String is in Double Quotes
     getch();
    }
     
    OUTPUT: 
    String is: This is an example string
    String is: String
    

     

    478

    Users Joined

    mahek thakur
    8 months ago
    sowmya
    8 months ago
    biraja
    9 months ago
    Diana R
    9 months ago
    P Annapoorani
    9 months ago
    X
    Explain about the string Input/ Output functions with example?
    X
    Explain about the string Input/ Output functions with example?

    Reading and Writing strings:-

    C language provides several string handling functions for input and output.

    String Input/Output Functions:-

    C provides two basic methods to read and write strings. Using formatted input/output functions and using a special set of functions.

    Reading strings from the terminal:-

    (a) formatted input function:- scanf can be used with %s format specification to read a string.

    Ex:- char name[10];

    scanf(“%s”,name);

    Here don‘t use‘&’ because the name of the string is a pointer to the array. The problem with scanf is that it terminates its input on the first white space it finds.

    Ex:- NEW YORK

    Reads only NEW (from above example).

    (b) Unformatted input functions:-

    (1) getchar():- It is used to read a single character from the keyboard. Using this function repeatedly we may read an entire line of text

    Ex:- char ch=‘z’;

    ch=getchar();

    (2) gets():- It is a more convenient method of reading a string of text including blank spaces.

    Ex:- char line[100];

    gets(line);

    Writing strings on to the screen:-

    (1) Using formatted output functions:- printf with %s format specifier we can print strings in different formats on to screen.

    Ex:- char name[10];

    printf(“ s ”,name);

    Ex:- char name[10];

    printf(“0.4”,name);   

    J A N U

    /* If name is JANUARY prints only 4 characters ie., JANU */

    Printf(“10.4”,name);

                J A N U

    printf(“-10.4”,name);

    J A N U            

    (2) Using unformatted output functions:-

    (a) putchar():- It is used to print a character on the screen.

    Ex:- putchar(ch);

    (b) puts():- It is used to print strings including blank spaces.

    Ex:- char line[15]=“Welcome to lab”;

    puts(line);

    X
    Explain about the string Input/ Output functions with example?

    String I/O: gets() and puts()

    One of the first operations we may need for strings is the ability to read or write strings from the standard input or to the standard output.

    Library function, gets(s) reads a string from the standard input into an array pointed to by s and, puts(s) writes a string pointed to by s to the standard output.

    The prototypes for these functions declared in stdio.h, are:

    STRING gets(STRING string);

    int puts(STRING string);

    gets() function: Accepts String

    Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

    The syntax for Accepting String : gets( )

    If reading is successful, gets() returns the pointer to the string; otherwise, it returns a NULL pointer, i.e. a pointer whose value is zero. A returned value of NULL usually implies an end of the file. When gets()reads a string, it reads the input characters until a newline is read, discards the newline, appends a NULL character to the string, and stores the string where s points. 

    #include
    void main()
    {
    char name[20];
    printf("\nEnter the Name : ");
    gets(name);
    }
    

    puts() function: Printing String

    Print character string to the standard output (stdout).

    The syntax for Printing String: puts()

    Similarly, puts() outputs the string, s, after stripping the NULL and appending a newline. It returns the last character value output if successful; otherwise, it returns EOF.

    #include< stdio.h>
    #include< conio.h>
    void main()
     {
     char string[] = "This is an example string\n";
     puts(string); // String is variable Here
     puts("String"); // String is in Double Quotes
     getch();
    }
     
    OUTPUT: 
    String is: This is an example string
    String is: String
    

     

    EngineersHub Logo
    x
    Loading...