{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# day17 - Converting strings to integers The string tokenizer...

This preview shows pages 1–8. Sign up to view the full content.

1 Converting strings to integers The string tokenizer returns character pointers (i.e., strings). If the string is actually a number, you might want to convert it. Include stdlib.h returns the string parameter as an double ; returns 0.0 if no conversion possible atof( str ) returns the string parameter as an int ; returns 0 if no conversion possible atoi( str ) Description Function name

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Exercise Write a function named sum that accepts a non- const string as a parameter. Assume the text is a series of numbers. The function returns the sum of the numbers. For example, if the parameter represents the string " 3.14 4 61.3 8.3 9 " then the function should return 85.74 .
3 Solution double sum(char *string) { double total = 0.0; char *pToken = strtok(string, " "); while (pToken != NULL) { total += atof(pToken); // get next token pToken = strtok(NULL, " "); } } return total; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Documentation
5 Reading documentation You can't learn everything there is to know in school. The world is constantly changing with new information and ideas. When you work on a new project, you will have to teach yourself by readings books and referring to documentation.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Man pages A good example of documentation for C functions are man pages. The man is short for "manual". Man pages are broken down into the following sections: NAME SYNOPSIS DESCRIPTION EXAMPLES SEE ALSO
7 A example man(ual) page SYNOPSIS #include <string.h>;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}