Previous: 11.2 Library String Functions
Up: 11.2 Library String Functions
Next: 11.2.2 String Manipulation: strlen() and strcpy()
Previous Page: 11.2 Library String Functions
Next Page: 11.2.2 String Manipulation: strlen() and strcpy()

11.2.1 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. For example, if we had a task:

STR0: Read strings until end of file, convert each string to upper case, and print the modified string.

we could easily write an algorithm for the task:

while not EOF, read a string
         convert string to upper case
         print string

To implement this algorithm, we need three functions: read a string from standard input stream, write a string to standard output, and convert a string to upper case. We have shown crude versions of these first two functions in the previous, section; however, the standard library provides these operations as well. 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);

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 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. 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. Note, the arguments to these functions and the return value from gets() are character pointers, i.e. equivalent to our STRING data type, and we can consider them as such. The argument of gets() MUST be a string; otherwise, the function attempts to store characters wherever the argument points, which can create a possibly fatal error when the program executes.

We will write and use a function, ucstr(), which converts a string to upper case. The whole program is simple: it reads a string, converts it to upper case, and prints it; and is shown in Figure 11.2

In the driver, the loop expression reads a line into s; if successful, the returned value is a non-zero pointer, s, and the loop is executed. In the loop body, the string, s, is converted to upper case, and printed. The function, ucstr(), converts a string to upper case by traversing the string and converting each character to upper case using library routine, toupper(), which returns the upper case version of its argument if it is a lower case letter; otherwise it returns the argument unchanged.

Sample Session:

The above program reads lines until end of file. As a slight variation on this task, sometimes it is desirable to loop until a blank line is entered. Here is a loop that copies lines until a blank line is entered:

while (*gets(s))
    puts(s);

Assuming that a line is read successfully, gets() returns s. The expression, *gets(), is the same as *s, which is the first character in the string, s. As long as the first character of s has a non-zero value, the loop continues. When the first character is a NULL, the loop terminates. If a blank line is entered by typing a RETURN, gets() reads an empty string and the loop terminates.

We can also use gets() in a menu driven program which requires the user to enter either a single character or a command line. In our previous menu driven programs in Chapter , we saw that reading a single command character required that the keyboard buffer be flushed of the newline character before reading the next command. If only one character is to be read, or if the first character of a command line is sufficient to identify a command, then it is simpler to read the entire line using gets(), which strips the newline character from the input line, and then examine only the first character of the input string. Here is a loop for a menu driven program driver:

printf("H(elp, Q(uit, D(isplay\n");
while (gets(s)) {
    switch (toupper(*s)) {

case 'H': help(); break;

case 'Q': exit(0);

case 'D': display(); break;

default: ; }

printf("H(elp, Q(uit, D(isplay\n"); }

The loop reads an input string, s, and passes the first character of s, *s to toupper() which converts it to upper case. One of the cases in the switch is selected and an appropriate function is executed. The loop repeats until gets() returns end of file.

We may now use library functions, gets() and puts(), in place of functions we have previously written ourselves to read and write strings. Remember, gets() reads an entire line of input text into a string; replacing the newline with a NULL. Likewise, puts() prints an entire NULL terminated string; adding a newline at the end.



Previous: 11.2 Library String Functions
Up: 11.2 Library String Functions
Next: 11.2.2 String Manipulation: strlen() and strcpy()
Previous Page: 11.2 Library String Functions
Next Page: 11.2.2 String Manipulation: strlen() and strcpy()

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:04:57 HST 1994