为什么 scanf("%[^\n]\n", 滴度);问我第三个输入
Why does scanf("%[^\n]\n", titre); ask me for a third input
我在 Win10 上,目前正在学习 C,我完全不理解我的输出 (EKEKEKEKE) 对于一个简单的练习,要求输入并给出相同的字符串作为输出,但没有元音字母。
inputBLABLABLA
EKEKEKE
outputB.L.B.L.B.L.
inputBLABLABLA
outputK.K.K.
int main()
{
for (int i = 0; i < 2; i = i + 1)
{
printf("input");
char titre[101] = {0};
scanf("%[^\n]\n", titre);
int j = 0;
char t = titre[0];
printf("output");
while ((t != '[=11=]') && (j < 101))
{
if ((t != 'A')
&& (t != 'E')
&& (t != 'I')
&& (t != 'O')
&& (t != 'U')
&& (t != 'Y')
&& (t != ' '))
{
printf("%c.", t);
}
j = j + 1;
t = titre[j];
}
if (i == 0)
{
printf("\n");
}
}
}
嗯,问题确实出在 scanf()
正则表达式上。主要是你正在吃除换行符之外的所有字符,直到你遇到换行符。然而,匹配完所有 scanf()
仍然没有 return 因为它仍然 'matching' 更多。通过更改该表达式,使其不匹配换行符而是匹配任意字符(应该是换行符),我能够让它按照您的预期进行响应。我还稍微修改了您的代码,将一些功能移至另一个功能:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define INPUT_COUNT 2
#define BUFFER_SIZE 100
int is_vowel (char c)
{
const char *vowels = "AEIOUY ";
for (const char *s = vowels; *s != '[=10=]'; s++) {
if (toupper(c) == *s) {
return 1;
}
}
return 0;
}
int main ()
{
for (int i = 0; i < INPUT_COUNT; i++) {
char c, titre[BUFFER_SIZE + 1] = {0};
// Scan in a line.
printf("Input:\t");
// %100 <- Limit 100 characters. %c <- Trick to get around newline issue
scanf(" %100[^\n]%c", titre, &c);
printf("Output:\t");
for (int j = 0; j < BUFFER_SIZE && titre[j] != '[=10=]'; j++) {
if (!is_vowel(titre[j])) {
printf("%c.", titre[j]);
}
}
putchar('\n');
}
}
您应该使用 gets 来读取这样的字符串:
int main()
{
for (int i = 0; i < 2; i = i + 1)
{
printf("input :");
char titre[101] = {0};
gets(titre);
int j = 0;
char t = titre[0];
printf("output :");
while ((t != '[=10=]') && (j < 101))
{
if ((t != 'A')&&(t != 'E')&&(t != 'I')&&(t != 'O')&&(t != 'U')&&(t != 'Y')&&(t != ' '))
{
printf("%c.", t);
}
j = j + 1;
t = titre[j];
}
if (i == 0)
{
printf("\n");
}
}
}
我在 Win10 上,目前正在学习 C,我完全不理解我的输出 (EKEKEKEKE) 对于一个简单的练习,要求输入并给出相同的字符串作为输出,但没有元音字母。
inputBLABLABLA
EKEKEKE
outputB.L.B.L.B.L.
inputBLABLABLA
outputK.K.K.
int main()
{
for (int i = 0; i < 2; i = i + 1)
{
printf("input");
char titre[101] = {0};
scanf("%[^\n]\n", titre);
int j = 0;
char t = titre[0];
printf("output");
while ((t != '[=11=]') && (j < 101))
{
if ((t != 'A')
&& (t != 'E')
&& (t != 'I')
&& (t != 'O')
&& (t != 'U')
&& (t != 'Y')
&& (t != ' '))
{
printf("%c.", t);
}
j = j + 1;
t = titre[j];
}
if (i == 0)
{
printf("\n");
}
}
}
嗯,问题确实出在 scanf()
正则表达式上。主要是你正在吃除换行符之外的所有字符,直到你遇到换行符。然而,匹配完所有 scanf()
仍然没有 return 因为它仍然 'matching' 更多。通过更改该表达式,使其不匹配换行符而是匹配任意字符(应该是换行符),我能够让它按照您的预期进行响应。我还稍微修改了您的代码,将一些功能移至另一个功能:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define INPUT_COUNT 2
#define BUFFER_SIZE 100
int is_vowel (char c)
{
const char *vowels = "AEIOUY ";
for (const char *s = vowels; *s != '[=10=]'; s++) {
if (toupper(c) == *s) {
return 1;
}
}
return 0;
}
int main ()
{
for (int i = 0; i < INPUT_COUNT; i++) {
char c, titre[BUFFER_SIZE + 1] = {0};
// Scan in a line.
printf("Input:\t");
// %100 <- Limit 100 characters. %c <- Trick to get around newline issue
scanf(" %100[^\n]%c", titre, &c);
printf("Output:\t");
for (int j = 0; j < BUFFER_SIZE && titre[j] != '[=10=]'; j++) {
if (!is_vowel(titre[j])) {
printf("%c.", titre[j]);
}
}
putchar('\n');
}
}
您应该使用 gets 来读取这样的字符串:
int main()
{
for (int i = 0; i < 2; i = i + 1)
{
printf("input :");
char titre[101] = {0};
gets(titre);
int j = 0;
char t = titre[0];
printf("output :");
while ((t != '[=10=]') && (j < 101))
{
if ((t != 'A')&&(t != 'E')&&(t != 'I')&&(t != 'O')&&(t != 'U')&&(t != 'Y')&&(t != ' '))
{
printf("%c.", t);
}
j = j + 1;
t = titre[j];
}
if (i == 0)
{
printf("\n");
}
}
}