/* echofirstword.c * Neal Nelson 2009.10.02 * * Echo first word of a line and strip leading spaces. * Overwrite the buffer provided by the caller. * Use strspn to skip leading spaces. * */ #include #include #define MAXLINE 80 main() { char buf[MAXLINE]; while (fgets(buf, MAXLINE, stdin) != NULL ) { getArg(buf); printf ("%s\n", buf); } } getArg(char *buf) { char* p; char ws[] = " \n\t\r\f\v"; for (p = buf + strspn(buf,ws); !isspace(*p) && *p != '\0'; p++) { *buf++ = *p; } *buf = '\0'; }