The string is nothing but an array of characters. Similar as character I/O, C have standard library functions for input/output operations of strings. They are
gets(char *s)
and puts(const char *s)
.
To read a string from standard input, use
gets()
as give below.
- char string[20];
- gets(string);
gets
reads string into the passed character array until a newline or EOF is encountered in the standard input. For instance, it reads all the characters and save it in the passed character array until you press "enter" key.
To write a string to the standard output, use
puts()
as give below.
- char string[] = "This is a string.";
- puts(string);
puts
writes a string to standard output until a string terminator is encountered. Example of String Input/Output
- #include<stdio.h>
- void main()
- {
- char s[100];
- printf("Enter a string: ");
- gets(s);
- puts(s);
- }
- Output:
- Enter a string: This is a string
- This is a string
The above program reads a line as input and prints the line into the screen.
No comments:
Post a Comment