Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday 20 August 2018

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

I have done this question by two methods



  • By using loops


declare
counts number :=0;
counts1 number :=0;
str1 varchar2(500) :=LOWER('This is the string');
begin
  for i in 1..length(str1)
    loop
     if(substr(str1,i,1) in ('a','e','i','o','u'))
      then
        counts := counts + 1;
          else
         counts1 := counts1 + 1;
      end if;   
    end loop;
    dbms_output.put_line('Input : '||str1);
    dbms_output.put_line('Total vovels :' ||counts);
    dbms_output.put_line('Total consonants :' ||counts1);

end;     



  • By using regular expressions


declare
counts number := 0;
counts1 number := 0;
str1 varchar2(500) := lower('This is the string');
begin
select length(regexp_replace(str1,'[^aeiou]')) as total_vovels into counts from dual;
select length(regexp_replace(str1,'[aeiou]'))  as total_consonants into counts1 from dual;
dbms_output.put_line('Total vovels :'||counts);
dbms_output.put_line('Total consonants :'||counts1);
end;

No comments:

Post a Comment