Search code examples
cstringcharprintfgetchar

Program that prints out characters in C


I have created a program that asks the user to input some text. The program then outputs each character on a new line and states whether the letter is vowel or punctuation.

The problem im having that when i run the program - when it outputs letters that are vowels or punctuation it prints them out twice. Can anyone help ? would be appreciated.

Code:

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;
}

Solution

  • This:

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

    Should be:

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