Next Time Read Comma Again Using Consonant Plus Vowel Linking
C plan to count the full number of vowels and consonants in a string – In this article, nosotros will discuss the multiple means to count the total number of vowels and consonants in a string in C programming.
Suitable examples and sample programs have also been added and then that yous can understand the whole thing very clearly. The compiler has as well been added with which you tin execute information technology yourself.
The ways mentioned in this piece are equally follows:
- Using Standard Method
- Using Office
- Using Recursion
- Using Pointers and While Loop
A cord is nil but an array of characters. The value of a cord is adamant by the terminating character. Its value is considered to be 0.
Firstly, you need to enter a string whose vowels and consonants y'all need to determine.
Here, the string "how-do-you-do earth" is entered.
Equally you can see, at that place are:
Vowels: 3
Consonants: 7
Hence, the numbers will be printed accordingly.
Thus, the different methods to do and so in C programming are every bit follows:
Using Standard Method
- Read the entered cord and store the string into the variable 's' using gets(south) office.
2)For loop iterates through string 's' with the structure for(i=0;s[i];i++),
a)If the ASCII value of s[i] is in the range of 65 to 90 or 97 to 122 then check s[i] is equal to any ane of the vowels(a,e,i,o,u).If due south[i] is equal to whatever vowel then increase the vowel count, otherwise increment the consonant count.
Echo this step up to end of the string.
three)Impress the number of vowels and consonants present in the cord.
| 1 2 3 4 v 6 7 8 nine ten xi 12 13 14 15 16 17 18 19 xx 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #include <string.h> int master ( ) { char southward [ 1000 ] ; int i , vowels = 0 , consonants = 0 ; printf ( "Enter the string : " ) ; gets ( s ) ; for ( i = 0 ; south [ i ] ; i ++ ) { if ( ( s [ i ] >= 65 && s [ i ] <= 90 ) || ( s [ i ] >= 97 && southward [ i ] <= 122 ) ) { if ( s [ i ] == 'a' || s [ i ] == 'due east' || s [ i ] == 'i' || southward [ i ] == 'o' || s [ i ] == 'u' || s [ i ] == 'A' || s [ i ] == 'E' || s [ i ] == 'I' || s [ i ] == 'O' || s [ i ] == 'U' ) vowels ++ ; else consonants ++ ; } } printf ( "vowels = %d\n" , vowels ) ; printf ( "consonants = %d\due north" , consonants ) ; return 0 ; } |
Output:
| Enter the cord : hello earth vowels = iii consonants = seven |
Using Function – Count Total No of Vowels & Consonants
- The main() calls the stringcount() part, passing the string as an argument to the function.
ii)The office stringcount(char *s) counts the number of vowels and consonants nowadays in the string.
a)The office checks the each character's ASCII value is inside the range of alphabets or not.
b)If the character is an alphabet then it compares the character with vowels. If the character is a vowel then increase the vowel count, otherwise increase the consonant count.
These a,b steps will be repeated upwardly to finish of the cord using for loop with the construction for(i=0;s[i];i++).
three)The function stringcount(char *s) prints the vowels count and consonant count.
| 1 2 3 4 5 vi 7 eight 9 ten 11 12 13 14 15 sixteen 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <string.h> void stringcount ( char * s ) { int i , vowels = 0 , consonants = 0 ; for ( i = 0 ; due south [ i ] ; i ++ ) { if ( ( s [ i ] >= 65 && s [ i ] <= 90 ) || ( s [ i ] >= 97 && due south [ i ] <= 122 ) ) { if ( southward [ i ] == 'a' || s [ i ] == 'e' || s [ i ] == 'i' || s [ i ] == 'o' || s [ i ] == 'u' || s [ i ] == 'A' || southward [ i ] == 'Due east' || s [ i ] == 'I' || s [ i ] == 'O' || s [ i ] == 'U' ) vowels ++ ; else consonants ++ ; } } printf ( "vowels = %d\north" , vowels ) ; printf ( "consonants = %d\north" , consonants ) ; } int main ( ) { char s [ 1000 ] ; printf ( "Enter the cord: " ) ; gets ( s ) ; stringcount ( s ) ; } |
Output:
| Enter the cord : goutham vowels = 3 consonants = four |
Using Recursion
- The main() function calls the stringcount(char *s) function by passing the string equally an statement.
2)The part stringcount(char *s)
initialize vowels=0,consonants=0.
a)If due south[i] is null then print the vowels count and consonants count.
b)if due south[i] is not cipher so check southward[i] is an alphabet or not, based on ASCII value.
b.one)If s[i] is an alphabet then compare southward[i] with vowels if information technology is a vowel then increment the vowel count otherwise increase the consonant count.
b.2)i value volition be increased by 1. The part calls itself.
The function calls itself recursively upward to s[i] is equal to nil.
| i 2 3 4 five 6 7 viii nine 10 11 12 13 14 15 16 17 18 xix 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h> #include <cord.h> void stringcount ( char * due south ) { static int i , vowels = 0 , consonants = 0 ; if ( ! s [ i ] ) { printf ( "vowels = %d\n" , vowels ) ; printf ( "consonants = %d\north" , consonants ) ; return ; } else { if ( ( south [ i ] >= 65 && due south [ i ] <= 90 ) || ( southward [ i ] >= 97 && south [ i ] <= 122 ) ) { if ( s [ i ] == 'a' || s [ i ] == 'e' || due south [ i ] == 'i' || s [ i ] == 'o' || due south [ i ] == 'u' || s [ i ] == 'A' || s [ i ] == 'E' || south [ i ] == 'I' || s [ i ] == 'O' || due south [ i ] == 'U' ) vowels ++ ; else consonants ++ ; } i ++ ; stringcount ( s ) ; } } int chief ( ) { char due south [ 1000 ] ; printf ( "Enter the cord: " ) ; gets ( south ) ; stringcount ( s ) ; } |
Output:
| Enter the string : alphabets vowels = 3 consonants = half-dozen |
Using Pointers And While Loop
- p is the pointer variable which points the cord s.
2)While loop checks first character of the string existed or non.If information technology has existed and then check information technology is an alphabet or not.
a)If information technology has existed then check it is alphabet or not. If information technology is an alphabet then bank check it is vowel or consonant. If it is a vowel and then increase the vowel count otherwise increase the consonant count.
b)Increment the p count, then pointer variable points the next graphic symbol of the cord. Then check the while condition for the next character of the string.
Repeat until the graphic symbol at pointer variable p becomes nix.
3)Print the vowels count and consonants count.
| 1 2 three 4 5 6 seven 8 ix 10 11 12 13 14 xv 16 17 18 xix 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <string.h> int main ( ) { char s [ thousand ] , * p ; int vowels = 0 , consonants = 0 ; printf ( "Enter the string : " ) ; gets ( due south ) ; p = south ; while ( * p ) { if ( ( * p >= 65 && * p <= 90 ) || ( * p >= 97 && * p <= 122 ) ) { if ( * p == 'a' || * p == 'eastward' || * p == 'i' || * p == 'o' || * p == 'u' || * p == 'A' || * p == 'Eastward' || * p == 'I' || * p == 'O' || * p == 'U' ) vowels ++ ; else consonants ++ ; } p ++ ; } printf ( "vowels = %d\n" , vowels ) ; printf ( "consonants = %d\n" , consonants ) ; render 0 ; } |
Output:
| Enter the string : AEIOU aeiou vowels = 10 consonants = 0 |
Source: https://javatutoring.com/c-program-count-vowels-consonants-in-string/
0 Response to "Next Time Read Comma Again Using Consonant Plus Vowel Linking"
Post a Comment