Programming Example - get_word (fixed)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
char *
get_word(void) {
int i, c;
char *w;
char word[64];
for (i = 0; i < sizeof word - 1; i++) {
c = getchar();
if (c == EOF || isspace(c))
break;
word[i] = c;
}
word[i] = '\0';
w = malloc(strlen(word) + 1);
if (w == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
strcpy(w, word);
return w;
}
int
main(int argc, char *argv[]) {
char *s, *t;
printf("Enter first word: ");
s = get_word();
printf("Enter second word: ");
t = get_word();
printf("You entered '%s' and '%s'\n", s, t);
return 0;
}
Index