No. of vowels and consonants in a given string in PL/SQL
Prerequisite – PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.
Given a string and the task is to find the number of vowels and consonants present in the string.
Examples:
Input: str = 'Ramesh' Output: Vowels = 2, Consonants = 4 Input: str = 'Ramesh is a Geek' Output: Vowels = 6, Consonants = 7
Approach is to consider a character one by one and maintain a separate count for both vowels and consonants.
Below is the required implementation:
DECLARE -- Here variable V is varchar datatype -- and flag variable is number datatype -- variable c is char datatype . v VARCHAR2(400) := 'Ramesh is a Geek' ; noofvowels NUMBER := 0; noofconsonants NUMBER := 0; C CHAR ; BEGIN FOR i IN 1..Length(v) LOOP c := Substr(v, i, 1); -- Check if the current character is vowel IF c IN ( 'A' , 'E' , 'I' , 'O' , 'U' ) OR c IN ( 'a' , 'e' , 'i' , 'o' , 'u' ) THEN noofvowels := noofvowels + 1; -- Else current character is a consonant except space ELSE IF c NOT IN ( ' ' ) THEN noofconsonants := noofconsonants + 1; END IF; END IF; END LOOP; dbms_output.Put_line( 'No. of Vowels: ' || noofvowels); dbms_output.Put_line( 'No. of Consonants: ' || noofconsonants); END ; -- Program End |
Output :
No. of Vowels: 6 No. of Consonants: 7
Recommended Posts:
- PLSQL | ABS Function
- PLSQL | COS Function
- PLSQL | SIN Function
- PLSQL | LEAST Function
- PLSQL | TAN Function
- PLSQL : || Operator
- PLSQL | CHR Function
- PLSQL | LOG Function
- PLSQL | MOD Function
- PLSQL | LN Function
- Difference between SQL and PLSQL
- PLSQL | EXP Function
- PLSQL | DBTIMEZONE Function
- PLSQL | RPAD Function
- PLSQL | EXTRACT Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.