C Tutorial
Custom data type can be created using predefined data type in c programmin.
typedef int bool; enum { false, true };
Initialization starts from 0, so false is 0 and true is 1.
This C program is used to find the number of vowels used in the input string.
typedef and enum keywords are used to define new user type bool.
#include <stdio.h> #include <string.h> typedef int bool; enum { false, true }; bool is_vowel(char); int main() { char str[100]; int i, count = 0; printf("Enter a string: "); gets(str); for(i = 0; str[i] != '\0'; i++) { if(is_vowel(str[i]) == true) { //if a character is vowel count++; } } printf("Number of vowels in string: %d\n", count); return 0; } bool is_vowel(char c) { switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return true; default: return false; } }Compilation:
When compiling this program, throws below warning message due to gets function usage in the program but we can execute the program.
$ cc no_of_vowels.c no_of_vowels.c: In function ‘main’: no_of_vowels.c:15:3: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] gets(str); ^~~~ /tmp/ccuJYVvE.o: In function `main': no_of_vowels.c:(.text+0x2b): warning: the `gets' function is dangerous and should not be used.Output:
$ ./a.out Enter a string: alphabet Number of vowels in string: 3 $ ./a.out Enter a string: testing inprogress Number of vowels in string: 5
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page