私の理解では、fgets()
はキャリッジリターン(\n
)まで入力をスキャンしますが、このコードは別の方法を示します。私は余分なラインがどこから来るのか分からない。誰か説明できますか?Cでこのfgets()の動作を説明してください
#include <stdio.h>
#include <string.h>
#define BUF_SZ 256
void reverse(char str[]) {
/* fill in the function body please */
for (int i = 0, j = strlen(str) - 1; i < strlen(str)/2; i++, j--)
{
char temp; // perform the swap
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main() {
char mesg[BUF_SZ];
printf("Enter a message please=> ");
fgets(mesg,BUF_SZ,stdin);
reverse(mesg);
printf("%s\n",mesg);
return 0;
}
[fgetsから改行を削除する]いくつかの方法があります(https://stackoverflow.com/questions/2693776) – user3386109