-3

This program just count the the vowels in the word you type into the program. but I want this program accept only characters not a number. Is there a way?

#include <stdio.h>
#define MAX 50

int countVowel(char[]);

void main()
{
    char text[MAX];
    int cVowel, sum;
    printf("Enter text : ");
    scanf("%s", text);
    cVowel = countVowel(text);
    printf("Text : [%s] has %d vowels", text, cVowel);

}

int countVowel(char t[])
{
    int i = 0, count = 0;
    while (i < MAX && t[i] != '\0')
    {
        if (t[i] == 'A' || t[i] == 'a' || t[i] == 'E' || t[i] == 'e'
                || t[i] == 'I' || t[i] == 'i' || t[i] == 'O' || t[i] == 'o'
                || t[i] == 'u' || t[i] == 'U')

            count++;
        i++;

    }
    return (count);

}

I've tried using atoi but it didn't work :/

LPs
  • 16,045
  • 8
  • 30
  • 61
Pao44445
  • 1
  • 1
  • 1
    Possible duplicate of [Determine if char is a num or letter](http://stackoverflow.com/questions/8611815/determine-if-char-is-a-num-or-letter) –  Mar 17 '17 at 08:51
  • 1
    What do you mean by "accept"? Ignore any other character? Return an error when the input text contains anything but a character? –  Mar 17 '17 at 08:53

2 Answers2

1

You can use strpbrk:

Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.

int main(void) /* void main is not a valid signature */
{
    char text[MAX];
    int cVowel, sum;

    while (1) {
        printf("Enter text : ");
        scanf("%s", text);
        if (strpbrk(text, "0123456789") == NULL) {
            break;
        }
    }
    ...
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

@Keine Lust's answer is correct if you want to disregard an entire string if it contains numeric characters.

If you just want to ignore them, then while iterating over your string, check if the current character is >= 48 and <= 57 (as per the ASCII character table]. If that condition is true, then simply continue; the iteration.

Mayazcherquoi
  • 474
  • 1
  • 4
  • 12