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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 .
Background image of page 2
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; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Documentation
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
7 A example man(ual) page SYNOPSIS #include <string.h>; char *strtok(char *str, const char *delim); DESCRIPTION The strtok() function parses a string into a sequence of tokens. On
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/29/2009 for the course EE 142 taught by Professor Bensollimketkai during the Spring '09 term at Washington State University .

Page1 / 19

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online