Background
Just got started with C and now am testing a few pages and simple problems to get the syntax right.
On Programiz they provide a few very easy problems with solutions.
This program checks if user input is a Vowel or a Consonant or none of those. I solve slightly different, but would be glad to see criticism to details I may not even be aware of.
Environment
- For editing the code, I use Vim without any pluggins.
- For testing code I am using Google's picoc which is a runtime much like NodeJS or Python's where -in interactive mode- you type C code and it is evaluated right away, so you don't need compilation.
Code
The code is commented to reflect what I understand of each step. Double starred comments will produce documentation. Then I mostly use /* syntax */.
/* importing modules */
#include <stdio.h>/** standard C library for input output.*/
#include <stdbool.h>/** bool data type. Not sure if this is normally used.*/
#include <ctype.h>/** character types, isalpha() function.*/
/** main takes no input, sends no output.*/
void main(void){
/* set up */
bool isVowel; char letter; char vowels[] = "aeiouAEIOU";
/* take user input */
printf("Insert a character: ");
scanf("%c", &letter);//Store input at the address of letter.
/* first check this is an alphabetic character */
if(!isalpha(letter)) {printf("Not an alphabetic character\n"); }
int nOfVowels = sizeof(vowels)/sizeof(vowels[0]);
for(int a=0; a<nOfVowels; a++){
if(vowels[a]==letter){ isVowel=true; }
}
printf("%s\n", isVowel == false ? "Consonant": "Vowel");
}