/* echofirstword.c * Neal Nelson 2009.10.02 * * Echo first word of a line and strip leading spaces. * Overwrite the buffer provided by the caller. * */ #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; for (p = buf; isspace(*p); p++); /* skip leading spaces */ for ( ; !isspace(*p) && *p != '\0'; p++) /* get first word */ { *buf++ = *p; } *buf = '\0'; }