テストする方法の1つは、スペースであればすべての文字をチェックすることです。
#include <stdio.h>
#include <stdlib.h>
// TODO: you should use bool instead
int is_all_space(const char *s)
{
char *tmp;
tmp = (char *) s;
// you might check for '\n' instead in your case
// or even '\r'
while (*tmp != '\0') {
// TODO: this check will fail if the input has zero length
if (*tmp != ' ') {
return 0;
}
tmp++;
}
return 1;
}
// don't forget to put the argument in quotes if it contains spaces
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Usage: %s somestring\n", argv[0]);
exit(EXIT_FAILURE);
}
if (is_all_space(argv[1])) {
puts("Input is all-space and nothing else");
} else {
puts("Input it not all-space");
}
exit(EXIT_SUCCESS);
}
宿題ALERT - 私はあなたを教えてみましょう:if文、ループと1が関与する(素朴な方法)、あなたが取得した入力に 'scanf'(または' sscanf'を使用している場合はstrchr –
を使用する場合があります'fgets')、すべての空白である入力を受け付けません。 'scanf'の場合、' sscanf'の場合、入力を待つでしょう。戻り値は '1'ではありません。 –