Home > Net >  How to detect a alphabet if its a vowel or not with C? Here is my programme
How to detect a alphabet if its a vowel or not with C? Here is my programme

Time:09-20

How to detect a alphabet if its a vowel or not with C? Here is my programme

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int main(){
    char input;
    scanf("%c", &input);
    if(isupper(input)){
        printf("it's capital letter.\n");}
        
    else{
            printf("it's small letter.\n");}
    int C = ((int)input); //Change C from a char to sacii code
    int intarray[10] = {65, 69, 73, 79, 85, 97, 101, 105, 111, 117}; //array of vowel
    for(int i = 0; i < 10; i  ){
        if(C == intarray[i]){  //cheaking if C is a vowel or not
                printf("Yes\n"); //print if it is
                break;}

            else{
                printf("No\n"); //print if not
                break;}
    }

INPUT

o

OUTPUT

it's a small letter.

No

CodePudding user response:

You have a simple logic flow in your program.

The rules are:

  • It is a vowel if you found one element in your array that matches.
  • It is not a vowel if you do not find any matching value in your array.

That implies that you must search the whole array to find a vowel. Only after reaching the end of your array you can be sure it is not a vowel.

Besides that you should not use magic numbers if you want to store characters.

Try this:

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int main(void)
{
    char input;
    scanf("%c", &input);
    if (isupper(input)) {
        printf("it's capital letter.\n");
    }        
    else {
        printf("it's small letter.\n");}

        static const char vowels[] = "aeiouAEIOU";
        bool found = false;
        for (int i = 0; i < 10; i  ) {
            if (C == vowels[i]) {  //cheaking if C is a vowel or not
                found = true;
                break; // Do not continue search.
            }
        }

        if (found == true) {
            printf("Yes\n"); //print if it is a vowel
        }
        else {
            printf("No\n"); // No vowel found.
        }
    }
}

I changed that vowel array to a string, i.e. I added space for nul-terminator. That means you could use standard functions like strchr to check if a character is in that string. That for loop would not be required then:

   if (strchr(vowels, c) != NULL)
       found = true; // We have a vowel

CodePudding user response:

It is in your power to create your own lookup table that serves your needs.

Below is a LUT for 7-bit ASCII characters, populated to indicate the nature of the 128 ASCII values commonly used.

And there is a short bit of code that uses the LUT to determine the nature of a character (alpha or no, vowel or no) entered by the user.

#include <stdio.h>

char tbl[] =
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    "Spppppppbbppppppddddddddddpppppp"
    "pVCCCVCCCVCCCCCVCCCCCVCCCCCbpbpp"
    "pvcccvcccvcccccvcccccvcccccbpbpp" ;

int main() {

    int c = getchar();
    int cX = tbl[ c ];

    if( cX == 'C' || cX == 'V' )
        printf( "%c is a capital letter\n", c );
    else
    if( cX == 'c' || cX == 'v' )
        printf( "%c is a small letter\n", c );
    else
        printf( "%c is not a letter at all\n", c );

    if( cX == 'V' || cX == 'v' )
        printf( "%c is a vowel\n", c );
    else
        printf( "%c is not a vowel\n", c );

    return 0;
}

There should be nothing difficult in the code above. The only difficulty is running the program over-and-over.

So, using 3 strings, we can exercise the table a little bit more.

#include <stdio.h>

char tbl[] = // the 'crafted' LUT
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    "Spppppppbbppppppddddddddddpppppp"
    "pVCCCVCCCVCCCCCVCCCCCVCCCCCbpbpp"
    "pvcccvcccvcccccvcccccvcccccbpbpp" ;

int main() {
    char *smpl[] = { // 3 sample strings of characters
        "ABCDEFGHIJKLM01234NOPQRSTUVWXYZ(,=$)",
        "abcdefghijklm01234nopqrstuvwxyz[,=$]",
        "Twas BRILLIG and  the Slithey toves...\t",
    };
    const int nNum = sizeof smpl/sizeof smpl[0]; // how many?

    for( int i = 0; i < nNum; i   ) {
        puts( smpl[i] ); // show the string

        // now replace each character with its 'nature' (digit, vowel, etc.)
        for( int r = 0; smpl[i][r]; r   )
            smpl[i][r] = tbl[ smpl[i][r] ];

        puts( smpl[i] ); // show transformed string

        puts( "" ); // blank line separator
    }
    printf(
        "Legend:\n"
        "\tX - control char\n"
        "\tS - SP(ace)\n"
        "\tp - punctuation\n"
        "\tb - bracket/brace\n"
        "\td - digit\n"
        "\tC - consonant\n"
        "\tV - vowel\n"
    );

    return 0;
}

And the output of the 2nd version looks like...

ABCDEFGHIJKLM01234NOPQRSTUVWXYZ(,=$)
VCCCVCCCVCCCCdddddCVCCCCCVCCCCCbpppb

abcdefghijklm01234nopqrstuvwxyz[,=$]
vcccvcccvccccdddddcvcccccvcccccbpppb

Twas BRILLIG and  the Slithey toves...
CcvcSCCVCCVCSvccSSccvSCcvccvcScvcvcpppX

Legend:
        X - control char
        S - SP(ace)
        p - punctuation
        b - bracket/brace
        d - digit
        C - consonant
        V - vowel
  •  Tags:  
  • c
  • Related