Bradley Eversley
Bradley Eversley

Reputation: 31

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.

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.

Instead of "A is vowel, A is not consonant. l is not a vowel, l is a consonant" etc etc.

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?

Thanks!

Upvotes: 2

Views: 498

Answers (2)

Shawon
Shawon

Reputation: 51

input should be : a-z or A-Z

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

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

Useful tools are:

  • Arrays: continuous sequence of variables that can be accessed using index
  • %s format specifyer: have scanf() read strings (NUL-terminated sequence of characters)
  • for(initializer; condition; update) { body } loop:
    1. execute initializer
    2. repeat executing body and update while condition is true

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

Upvotes: 0

Related Questions