Programming in C: Unit II (b): Strings

Suppressing Input

Syntax with Example C Programs | Strings

The scanf() function can be used to read a field without assigning it to any variable. This is done by preceeding that field's format code with a *.

SUPPRESSING INPUT

The scanf() function can be used to read a field without assigning it to any variable. This is done by preceeding that field's format code with a *. For example, consider the example below:

scanf("%d*c%d", &hr, &min);

The time can be read as 9:05. Here the colon would be read but not assigned to anything. Therefore, assignment suppression is particularly useful when part of what is input needs to be suppressed.

Using a Scanset

The ANSI standard added the new scanset feature to the C language. Scanset is used to define a set of characters which may be read and assigned to the corresponding string. Scanset is defined by placing the characters inside square brackets prefixed with a %, as shown in the example below:

% ["aeiou"]

When we use the above scanset, scanf () will continue to read characters and put them into the string until it encounters a character that is not specified in the scanset. For example, consider the code given below.

#include <stdio.h>

int main()

{

char str[10];

printf("\n Enter string: );

scanf("% [aeiou]", str );

printf("The string is: %s", str);

return 0;

}

The code will stop accepting a character as soon as the user enters a character that is not a vowel.

However, if the first character in the set is a ^ (caret symbol), then scanf () will accept any character that is not defined by the scanset. For example, if you write

scanf("%[^aeiou]", str);

Then, str will accept characters other than those specified in the scanset, i.e., it will accept any non-vowel character. However, the caret and the opening bracket can be included in the scanset anywhere. They have a predefined meaning only when they are included as the first character of the scanset. So if you want to accept a text from the user that contains caret and opening bracket then make sure that they are not the first characters in the scanset. This is shown in the following example.

scanf("% [0123456789.^ [] () _+-$%&*]", str);

vs In the given example, str can accept any character enclosed in the opening and closing square brackets (including and [).lugins

The user can also specify a range of acceptable characters using a hyphen. This is shown in the statement given below:

scanf("% [a-z]", str);

Here, str will accept any character from small a to small z. Always remember that scansets are case sensi- tive. However, if you want to accept a hyphen then it must either be the first or the last character in the set.

Example 6.1

To better understand scanset try the following code with different inputs

#include <stdio.h>

int main()

{

char str[10];

printf("\n Enter string: ");

scanf("%% [A-Z]", str);

// Reads only upper case characters

printf("The string is: %s", str);

return 0;

}

A major difference between scanset and the string conversion codes is that scanset does not skip leading white spaces. If the white space is a part of the scanset, then scanset accepts any white space character, otherwise it terminates if a white space character is entered without being specified in the scanset.

Scanset may also terminate if a field width specification is included and the maximum number of characters that can be read has been reached. For example, the statement given below will read maximum 10 vowels. So the scanf statement will terminate if 10 characters have been read or if a non-vowel character is entered.

scanf("%10 [aeiou] ", str );

sscanf() Function

The sscanf function accepts a string from which to read input. It accepts a template string and a series of related arguments. The sscanf function is similar to scanf function except that the first argument of sscanf specifies a string from which to read, whereas scanf can only read from standard input. Its syntax is given as

sscanf (const char *str, const char *format, [p1, p2, ...]);

Here sscanf() reads data from str and stores them according to the parameter format into the locations given by the additional arguments. Locations pointed by each additional argument are filled with their corresponding type of value specified in the format string. Consider the example given below:

sscanf (str, "%d", &num);

Here, sscanf takes three arguments. The first is str that contains data to be converted. The second is a string containing a format specifier that determines how the string is converted. Finally, the third is a memory location to place the result of the conversion. When the sscanf function completes successfully, it returns the number of items successfully read.

Similar to scanf(), sscanf () terminates as soon as it encounters a space, i.e., it continues to read till it comes across a blank space.

Programming in C: Unit II (b): Strings : Tag: : Syntax with Example C Programs | Strings - Suppressing Input