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);
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
Recommended Questions
Useful Files
Users Joined
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);
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