Reading and writing strings

The following functions read and write strings:

gets ()

gets () reads a string of characters and places them at the address pointed to by its character pointer argument. When RETURN is entered, a null terminator is placed at the end of the string and the function returns. For example:

char str [25];
gets (str);


Program- uses of gets () function
#include <stdio.h>
void main ()
{
    char name [80];
    printf (“\Entered name:”);
    gets (name);
    printf (“Entered name is: %s\n”, name);
}

Output

Enter name: Meeta
Enter name is: Meeta


puts ()

puts () write its string argument to the screen, followed by a newline. It can not output numbers, but it is faster than printf () in outputting strings. It returns EOF if an error occurs. For example:

char str [] = “Hello world”;
puts (str);

this function will display the text “Hello world” on the screen.


Program- Use of puts () function
#include <stdio.h>
void main ()
{
    char name [80];
    puts (“Enter a string”);
    gets (name);
    puts (“Inputted string is”);
    puts (name);
}
Output

Enter a string
Bhubaneswar
Inputted strings is
Bhubaneswar



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext