将输出字符分析为元音,标点符号等

时间:2015-10-29 13:32:36

标签: c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char string[20];
    int len = strlen(string);

    //Asking the User to input some characters to use in the program
    printf("Enter a few characters please:\n");
    scanf("%s", string);

    //For loop to print each character of the string on a new line
    for (int i = 0; i < len; ++i)
    {
    printf("%c\n", string[i]);
    }

    //Test print to check the contents of the string
    printf("string's contents: '%s'\n", string);

    return 0;
}

这个代码我现在要求用户输入字符,现在我希望能够分析输出的每个字符,以确定它是元音,重复字符还是标点符号。例如,如果用户输入了AfsdhheE&#39;

程序应该输出:

A - 大写元音 F 小号 d H h - 重复的字符 e - 小写元音 E - 大写元音

提前感谢您的帮助。

// If statement to check whether the character is an upper case vowel
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
    printf("%c is an upper case vowel.\n", ch);

// If statement to check whether the character is a lower case vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    printf("%c is a lower case vowel.\n", ch);

这是我认为可能对元音有用的东西,但我不确定如何将它实现到循环中。

但是对于标点符号和重复的字符,我还没有办法做到这一点。

1 个答案:

答案 0 :(得分:0)

对于标点符号,请查看函数ispunct()。有关详细信息,请参阅http://www.tutorialspoint.com/c_standard_library/c_function_ispunct.htm

对于元音,这是一个很好的例子:http://cboard.cprogramming.com/c-programming/58821-isvowel-included-ctype-c-lang.html

重复的字符只是稍微复杂一些。如果这是C ++,我会使用地图来跟踪已经在数组中找到的字符。在C中,您可以保留一组唯一字符进行比较。