Open In App
Related Articles

No. of vowels and consonants in a given string in PL/SQL

Improve Article
Save Article
Like Article

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.

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:

<div id="highlighter_484083" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="keyword">DECLARE</code> </div><div class="line number2 index1 alt1"><code class="undefined spaces">    </code><code class="comments">-- Here variable V is varchar datatype   </code></div><div class="line number3 index2 alt2"><code class="undefined spaces">    </code><code class="comments">-- and  flag variable is number datatype    </code></div><div class="line number4 index3 alt1"><code class="undefined spaces">    </code><code class="comments">-- variable c is char datatype .   </code></div><div class="line number5 index4 alt2"><code class="undefined spaces">    </code><code class="plain">v              VARCHAR2(400) := </code><code class="string">'Ramesh is a Geek'</code><code class="plain">;  </code></div><div class="line number6 index5 alt1"><code class="undefined spaces">    </code><code class="plain">noofvowels     NUMBER := 0;  </code></div><div class="line number7 index6 alt2"><code class="undefined spaces">    </code><code class="plain">noofconsonants NUMBER := 0;  </code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="plain">C              </code><code class="keyword">CHAR</code><code class="plain">;  </code></div><div class="line number9 index8 alt2"><code class="keyword">BEGIN</code> </div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="keyword">FOR</code> <code class="plain">i </code><code class="color1">IN</code> <code class="plain">1..Length(v) LOOP  </code></div><div class="line number11 index10 alt2"><code class="undefined spaces">        </code><code class="plain">c := Substr(v, i, 1);  </code></div><div class="line number12 index11 alt1"><code class="undefined spaces"> </code> </div><div class="line number13 index12 alt2"><code class="undefined spaces">        </code><code class="comments">-- Check if the current character is vowel  </code></div><div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="plain">IF c </code><code class="color1">IN</code> <code class="plain">( </code><code class="string">'A'</code><code class="plain">, </code><code class="string">'E'</code><code class="plain">, </code><code class="string">'I'</code><code class="plain">, </code><code class="string">'O'</code><code class="plain">, </code><code class="string">'U'</code> <code class="plain">)  </code></div><div class="line number15 index14 alt2"><code class="undefined spaces">            </code><code class="color1">OR</code> <code class="plain">c </code><code class="color1">IN</code> <code class="plain">( </code><code class="string">'a'</code><code class="plain">, </code><code class="string">'e'</code><code class="plain">, </code><code class="string">'i'</code><code class="plain">, </code><code class="string">'o'</code><code class="plain">, </code><code class="string">'u'</code> <code class="plain">) </code><code class="keyword">THEN</code> </div><div class="line number16 index15 alt1"><code class="undefined spaces">          </code><code class="plain">noofvowels := noofvowels + 1;  </code></div><div class="line number17 index16 alt2"><code class="undefined spaces"> </code> </div><div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="comments">-- Else current character is a consonant except space  </code></div><div class="line number19 index18 alt2"><code class="undefined spaces">        </code><code class="keyword">ELSE</code> </div><div class="line number20 index19 alt1"><code class="undefined spaces">          </code><code class="plain">IF c </code><code class="color1">NOT</code> <code class="color1">IN</code> <code class="plain">( </code><code class="string">' '</code> <code class="plain">) </code><code class="keyword">THEN</code> </div><div class="line number21 index20 alt2"><code class="undefined spaces">            </code><code class="plain">noofconsonants := noofconsonants + 1;  </code></div><div class="line number22 index21 alt1"><code class="undefined spaces">          </code><code class="keyword">END</code> <code class="plain">IF;  </code></div><div class="line number23 index22 alt2"><code class="undefined spaces">        </code><code class="keyword">END</code> <code class="plain">IF;  </code></div><div class="line number24 index23 alt1"><code class="undefined spaces">    </code><code class="keyword">END</code> <code class="plain">LOOP;  </code></div><div class="line number25 index24 alt2"><code class="undefined spaces"> </code> </div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="plain">dbms_output.Put_line(</code><code class="string">'No. of Vowels: '</code> </div><div class="line number27 index26 alt2"><code class="undefined spaces">                         </code><code class="plain">|| noofvowels);  </code></div><div class="line number28 index27 alt1"><code class="undefined spaces"> </code> </div><div class="line number29 index28 alt2"><code class="undefined spaces">    </code><code class="plain">dbms_output.Put_line(</code><code class="string">'No. of Consonants: '</code> </div><div class="line number30 index29 alt1"><code class="undefined spaces">                         </code><code class="plain">|| noofconsonants);  </code></div><div class="line number31 index30 alt2"><code class="keyword">END</code><code class="plain">;  </code></div><div class="line number32 index31 alt1"><code class="comments">-- Program End   </code></div></div></td></tr></tbody></table></div>

Output :

No. of Vowels: 6
No. of Consonants: 7

Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.

Last Updated : 05 Jul, 2018
Like Article
Save Article
Similar Reads