Reading Strings From Terminal


Using scanf Function

The familiar input function scanf() can be used with %s format specification to read in a string of characters. Example :

char a[10];
scanf("%s",a);

The problem with the scanf function is that it terminates its input in the first white space it finds. White space includes spaces, tabs, carriages, form feeds, and newlines. So, if the following text line is typed in the terminal,

NEW YEAR

then only the string "NEW" will be read into the array address, since the blank space after the word "NEW" will terminates the reading of string.

The scanf() method automatically terminates the string that is read with a null character and therefore the character array should be large enough to hold the input string plus the null character. Note that, unlike previous scanf() calls, in the case of character arrays, the ampersand(&) is not required before the variable name.

The address array is created in the memory as shown below :

N E W \0 ? ? ? ? ? ?

Note that the unused locations are filled with the garbage value.


Reading a Line of Text

We've just noticed that with %s or %ws, Scanf can only read readable strings without white space. This is because they cannot be used to read a text with more than one word. However, the C-edited conversion code supports details of a format known as %[..] that can be used to read a line of different characters, including whitespace.

char line[80];
scanf("%[^\n]",line);
printf("%s",line);

The input from the keyboard will read a line and the same will appear on the screen. We will rarely use this method, since C supports an internal string function to do this.


Using getchar() and gets() Functions

We can use this function repeatedly to read single characters continuously from the input and place them in a character array. Thus, an entire line of text can be read and saved in an array. Reading is stopped when the new line character ('n') is inserted and the null character is inserted at the end of the string. The getchar() method call takes the form:

char ch;
ch = getchar();

Every time a character is read, it is assigned to its location in the string line and then tested for the newline character. When the newline character is read, the reading loop is terminated and the newline character is replaced by the null character to indicate the end of the character string.

Another and more convenient method of reading a string of text containing whitespace is to use the library function gets available in the header file. This is a simple function with one string parameter and called as under :

char ch[10];
gets(ch);

ch is a string variable declared properly. It reads characters into ch from the keyboard until a newline character is encountered and then appends a null character to the string. Unlike scanf, it does not skip whitespace. For example the code segment

char line(80);
gets(line);
printf("%s",line);

reads a line of text from the keyboard and displays it on the screen. The last two statements may be combined as follows :

printf("%s", gets(line));

C does not provide operators that work on strings directly. For instance we cannot assign one string to another directly. For example, the assignment statements :

string = "ABC";
string1 =  string2;