简体   繁体   English

获取 C 一次输入读取多个字符

[英]Get C to read more than one character in a single input

I'm new to C, and I'm looking to write a program that takes a word, say "Aloha" as input, notes all the vowels and consonants in the word, and counts the number of characters in the word.我是 C 的新手,我正在寻找一个程序,它接受一个单词,说“Aloha”作为输入,记下单词中的所有元音和辅音,并计算单词中的字符数。

I've created the functions to determine if a single character is a vowel or consonant in the Hawaiian language, but I don't know how to make the program read more than one character as input.我已经创建了函数来确定单个字符是夏威夷语中的元音字母还是辅音字母,但我不知道如何让程序读取多个字符作为输入。 For example, in the code below例如,在下面的代码中

#include <stdio.h>

int is_vowel(char);
int is_consonant(char letter);

int main() {

char letter;
int vowel, consonant;

    printf("Please input a Hawaiian word:");
    scanf("%c", &letter);

    vowel = is_vowel(letter);
    consonant = is_consonant(letter);

    if (vowel == 1) {

        printf("%c is a vowel. \n", letter);
    } else {

        printf("%c is not a vowel. \n", letter);
    }


    if (consonant == 1) {

    printf("%c is a consonant. \n", letter);
} else {

    printf("%c is not a consonant. \n", letter);
}

}

when the user inputs "Aloha", the output is "A is a vowel. A is not a consonant", then it stops.当用户输入“Aloha”时,output 是“A 是元音。A 不是辅音”,然后停止。

Instead of "A is vowel, A is not consonant. l is not a vowel, l is a consonant" etc etc.而不是“A 是元音,A 不是辅音。l 不是元音,l 是辅音”等等。

How do I get C to read all the characters in the input one at a time, and get the function (not included in the sample code) to check each of them?如何让 C 一次读取输入中的所有字符,并让 function (示例代码中未包含)逐个检查?

Thanks!谢谢!

input should be: az or AZ输入应为:az 或 AZ

#include <stdio.h>
#include <string.h>

int is_vowel(char ch) {
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') return 1;
    else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') return 1;
    else return 0;
};
int is_consonant(char ch) {
    if(is_vowel(ch) == 0) return 1;
    else return 0;
};

int main()
{
    char letter[100];
    int vowel, consonant;
    int i, length;

    printf("Please input a Hawaiian word: ");
    scanf("%[^\n]s", &letter);
    length = strlen(letter);

    for(i=0; i<length; i++) {

        vowel = is_vowel(letter[i]);
        consonant = is_consonant(letter[i]);
        if (vowel == 1) {
            printf("%c is a vowel. \n", letter[i]);
        }
        else {
            printf("%c is not a vowel. \n", letter[i]);
        }

        if (consonant == 1) {
            printf("%c is a consonant. \n", letter[i]);
        }
        else {
            printf("%c is not a consonant. \n", letter[i]);
        }
        printf("\n");
    }

    return 0;
}

Useful tools are:有用的工具是:

  • Arrays: continuous sequence of variables that can be accessed using index Arrays:可以使用索引访问的连续变量序列
  • %s format specifyer: have scanf() read strings (NUL-terminated sequence of characters) %s格式指定器:让scanf()读取字符串(以 NUL 结尾的字符序列)
  • for(initializer; condition; update) { body } loop: for(initializer; condition; update) { body }循环:
    1. execute initializer执行initializer
    2. repeat executing body and update while condition is true重复执行body并在condition为真时update

Try this:试试这个:

#include <stdio.h>

int is_vowel(char);
int is_consonant(char letter);

int main() {
    char letters[102401]; /* allocate enough size */
    int i;

    printf("Please input a Hawaiian word:");
    if (scanf("%102400s", letters) != 1) { /* read input with length limit and check if it succeeded */
        puts("input error");
        return 1;
    }

    for (i = 0; letters[i] != '\0'; i++) { /* loop over the letters read */
        char letter = letters[i];
        int vowel, consonant;

        vowel = is_vowel(letter);
        consonant = is_consonant(letter);

        if (vowel == 1) {

            printf("%c is a vowel. \n", letter);
        } else {

            printf("%c is not a vowel. \n", letter);
        }


        if (consonant == 1) {

            printf("%c is a consonant. \n", letter);
        } else {

            printf("%c is not a consonant. \n", letter);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM