在C中打印出字符的程序

时间:2015-10-23 21:09:06

标签: c string char printf getchar

我创建了一个程序,要求用户输入一些文本。程序然后在新行上输出每个字符,并说明该字母是元音还是标点符号。

我运行程序时遇到的问题 - 当它输出元音或标点符号的字母时,它会将它们打印两次。有人可以帮忙吗?将不胜感激。

代码:

int _tmain(int argc, _TCHAR* argv[])
{
    char string[200];
    char n1;

    printf("Enter some text: \n");

    while ((n1 = getchar()) != '\n') {

        if (n1 == 'a' || n1 == 'e' || n1 == 'i' || n1 == 'o' || n1 == 'u')
            printf("\b%c is a lowercase Vowel\n", n1);

        else if (n1 == 'A' || n1 == 'E' || n1 == 'I' || n1 == 'O' || n1 == 'U')
            printf("\b%c is a uppercase Vowel\n", n1);

        if (ispunct(n1))
            printf("%c is punctuation\n", n1);
        printf("%c\n", n1);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:3)

此:

   if (ispunct(n1))
        printf("%c is punctuation\n", n1);
        printf("%c\n", n1);

应该是:

   else if (ispunct(n1))
        printf("%c is punctuation\n", n1);
   else
        printf("%c\n", n1);