/* * Copyright 2011 Steven Gribble * * This file is the solution to an exercise problem posed during * one of the UW CSE 333 lectures (333exercises). * * 333exercises is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 333exercises is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 333exercises. If not, see . */ // Lecture 2 exercise 3: // // Write a function that: // - accepts a string as a parameter // - returns: // - the first whitespace-separated word in the string, as // a newly allocated string // - and, the size of that word #include // needed for the definition of NULL, printf #include // needed for malloc, free #include // needed for the definition of assert() #include // for strlen // We could use isspace() from the C standard library, // but I haven't taught you about that yet, so I'll write // my own is_space. int is_space(char c) { if ((c == ' ') || // space (c == '\f') || // form feed (c == '\n') || // newline (c == '\r') || // carriage return (c == '\t') || // horizontal tab (c == '\v')) { // vertical tab return 1; } return 0; } // FirstWord scans through "string" looking for the // first whitespace separated word in the string. // // If it finds one, it: // - malloc's space for that word // - copies the word into the malloc'ed space, including a NULL-term // - returns a pointer to the word through the "retword" return parameter // - returns the length of the returned word (>=0) // // If "string" is empty or NULL, returns -1 and doesn't malloc anything. // // If malloc fails, returns -2 to indicate out of memory. int FirstWord(char *string, char **retword) { int len, i, retlen; // test the degenerate case if ((string == NULL) || (string[0] == '\0')) { return -1; } len = strlen(string); retlen = len; // by default, copy the whole string // look for whitespace for (i=0; i