think I want to avoid gets
gets doesn't even exist in the current language spec.
You should definitely use fgets since your input is basically "line based" where you only want one word on the line.
One possibility:
Code:
#include <stdio.h>
int main() {
char line[1000], word[100], extra[2];
while (1) {
printf("Enter one word: ");
fgets(line, sizeof line, stdin);
extra[0] = '\0';
sscanf(line, "%99s%1s", word, extra);
if (extra[0] == '\0') break;
printf("I said ONE WORD!!! Try again...\n");
}
printf("You entered: %s\n", word);
return 0;
}
Another possibility:
Code:
#include <stdio.h>
#include <string.h>
int main() {
char line[1000], word[100];
while (1) {
printf("Enter one word: ");
fgets(line, sizeof line, stdin);
int pos = 0;
sscanf(line, "%99s%n", word, &pos);
if (line[strspn(line + pos, " \t\n") + pos] == '\0') break;
printf("I said ONE WORD!!! Try again...\n");
}
printf("You entered: %s\n", word);
return 0;
}