The line
ch[i] = '';
is wrong. The line is meaningless because
will already be there on ch[i]
because it is after exiting the loop with the condition i < strlen(ch)
.
The line should be
ch[i - size] = '';
to maintain consistency with the line inside the loop ch[i - size] = ch[i];
.
Also the line
scanf(" %[^
]s", &ch);
is wrong because char(*)[100]
is passed where char*
is expected and undefined behavior is invoked.
It should be
scanf(" %[^
]s", ch);
without the extra &
.
This version with checking is better:
if (scanf(" %[^
]s", &ch) != 1) {
fputs("read error
", stderr);
/* exit program (return from main(), using exit(), etc. */
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…