Previous: 11.2.3 String Operations: strcmp() and strcat()
Up: 11.2 Library String Functions
Next: 11.2.5 File I/O with Strings
Previous Page: 11.2.3 String Operations: strcmp() and strcat()
Next Page: 11.2.5 File I/O with Strings

11.2.4 String Conversion Functions

Besides the functions for manipulating strings discussed in the previous sections (and others not discussed, but presented in Appendix ), the standard library provides several functions for converting the character (ascii) information in a string to other data types such as integers or floats.

We will illustrate the use of one such function, atoi(), by modifying our function getint() that we wrote in Chapter . Recall, this function reads the next valid integer from the standard input character by character, skipping over any leading white space, converts the character sequence to an integer representation, and returns the integer value. The prototype for this function is:

int getint(void);
In our previous version of this function, we made it robust enough to detect when EOF or invalid (non-digit) characters are present in the input. Here we will extend the utility of getint() to read the next white space delimited item in the input, and convert it to integer form, this time allowing a leading + or - sign, and give the user the opportunity to re-enter data for illegal character errors.

GETINT: Write a program that reads only a valid integer. If there is an error in entering an integer, it detects the error and allows the user to re-enter the data.

The program driver is quite simple; it calls the function getint() that returns a valid integer read from the standard input. The driver then prints the integer returned by the function. Here is the algorithm for getint():

initialize valid to False

while not a valid string read a string s set valid to True if s represents a valid integer

if valid return an integer represented by the string s else print an error message

The function reads in the input as a string, and checks if it is a valid digit string for an integer. To check if a string s is a valid integer string, we examine whether it consists of only digits with, perhaps, a leading unary sign ( + or -). The following algorithm sets valid to True if s represents a valid integer:
if *s is '+' or '-'
         valid = digitstr(s + 1);
    else
         valid = digitstr(s);
If the first character of s is a unary sign, check the rest of the string (starting as s + 1) for all digits; otherwise check the entire string s for all digits.

If s is a valid digit string, the function returns an equivalent integer using the standard library function, atoi(). The call atoi(s) returns the integer represented by the string s. The function atoi() has the prototype (included in stdlib.h):

int atoi(STRING s);
If s is not a valid string, the user is prompted to type the input again.

To check if all characters in a string are digits, getint() uses the function digitstr(). The algorithm modules are combined and implemented in a program shown in Figure 11.7.

The driver gets an integer and prints it. The function getint() reads the next white space delimited string in the input using scanf(). If the first character is a unary operator, we check for digits string starting at the pointer s + 1; otherwise, we check starting at the pointer s. The flag, valid, stores the value returned by digitstr(). If valid is True, we use atoi() to return the integer represented by s; otherwise, we print a message prompting the user to re-enter the integer, flush any remaining characters on the input line, and read the new input. The flag valid is initialized to False, and the loop continues as long as valid remains False, i.e. as long as a valid integer is not entered.

The function digitstr() traverses the string until a NULL appears. If a non-digit is encountered anytime during traversal, it returns False; otherwise, at the end of traversal, it returns True. It uses the library function, isdigit() to check if a character is a digit.

A sample session is shown below:



Previous: 11.2.3 String Operations: strcmp() and strcat()
Up: 11.2 Library String Functions
Next: 11.2.5 File I/O with Strings
Previous Page: 11.2.3 String Operations: strcmp() and strcat()
Next Page: 11.2.5 File I/O with Strings

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