/* echofirstword.c * Neal Nelson 2009.10.02 * * Echo first word of a line and strip leading spaces. * Allocate and return a new string. * Use strspn to skip leading spaces. * */ #include #include #include #define MAXLINE 80 /* Don't forget prototypes */ char *getArg(char *buf); main() { char buf[MAXLINE]; char *p; while (fgets(buf, MAXLINE, stdin) != NULL ) { printf ("%s\n", getArg(buf)); } } char *getArg(char *buf) { char *p,*q; int len; char ws[] = " \n\t\r\f\v"; p = buf + strspn(buf,ws); len = strcspn(p,ws); q = (char *)malloc(len+1); strncpy(q, p, len); *(q+len) = '\0'; return q; }