Vowel Count

[XML] [Smart Pascal] [JavaScript] [Python] [output]

In this program we find by two different functions the number of vowels in the string supplied by the user. The first function counts separately the occurrences of each vowel in the string. The second function examines each letter in the string and determines whether or not it is in the string 'aeou'.

We used the Blockly Playground to generate JavaScript and Python code from the same blocks.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="procedures_defreturn" id="8^q/o{wLPc=gc|Lp:Pey" x="-688" y="-288">
    <mutation>
      <arg name="strInputWord"></arg>
    </mutation>
    <field name="NAME">CountVowels</field>
    <statement name="STACK">
      <block type="variables_set" id="d;)ef=OG!5-UW%Zy%f0Z">
        <field name="VAR">strInputWord</field>
        <value name="VALUE">
          <block type="text_changeCase" id="PJx;ss{/rBzrP*3@Ll=)">
            <field name="CASE">LOWERCASE</field>
            <value name="TEXT">
              <shadow type="text" id="If5CODK*2(Go0Oq43)%*">
                <field name="TEXT">abc</field>
              </shadow>
              <block type="variables_get" id="1-o*w#f{M|V^ahcy7/E,">
                <field name="VAR">strInputWord</field>
              </block>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="0mnynfdGMy$j-sGC[=PM">
            <field name="VAR">intVowelCount</field>
            <value name="VALUE">
              <block type="text_count" id="y*cKR6YAKX%TQ4;8-8/K">
                <value name="SUB">
                  <shadow type="text" id="o3RJz:,aZQ;-jXo2$n64">
                    <field name="TEXT">a</field>
                  </shadow>
                </value>
                <value name="TEXT">
                  <shadow type="text" id="}txQLs~Gq!/d8gKFE`Vn">
                    <field name="TEXT"></field>
                  </shadow>
                  <block type="variables_get" id="`99=a{+6LBB4!6}t-$4]">
                    <field name="VAR">strInputWord</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="variables_set" id="WeXcR$y;DH8JJ!X1B4nb">
                <field name="VAR">intVowelCount</field>
                <value name="VALUE">
                  <block type="math_arithmetic" id="vq[5WHt2sBTW(ynjE{]v">
                    <field name="OP">ADD</field>
                    <value name="A">
                      <shadow type="math_number" id="3N|8|VA9^ltZMdK9:+8f">
                        <field name="NUM">1</field>
                      </shadow>
                      <block type="variables_get" id="XZ_m57Dn!~(!Z/Qc[g@,">
                        <field name="VAR">intVowelCount</field>
                      </block>
                    </value>
                    <value name="B">
                      <shadow type="math_number" id="[;j@V$]uo-%k%wH3OE4D">
                        <field name="NUM">1</field>
                      </shadow>
                      <block type="text_count" id="Wz@PW[vWzlg0p.)xm`G_">
                        <value name="SUB">
                          <shadow type="text" id="|wThAyqg`C4b2!b(O~mz">
                            <field name="TEXT">e</field>
                          </shadow>
                        </value>
                        <value name="TEXT">
                          <shadow type="text" id="}txQLs~Gq!/d8gKFE`Vn">
                            <field name="TEXT"></field>
                          </shadow>
                          <block type="variables_get" id="T_%~l:w+Mf[%R_`.|`/^">
                            <field name="VAR">strInputWord</field>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="%h(#auUxr2)?j]9^d;uw">
                    <field name="VAR">intVowelCount</field>
                    <value name="VALUE">
                      <block type="math_arithmetic" id="FRb5tC/4pZ:ZD)v9cLkB">
                        <field name="OP">ADD</field>
                        <value name="A">
                          <shadow type="math_number" id="3N|8|VA9^ltZMdK9:+8f">
                            <field name="NUM">1</field>
                          </shadow>
                          <block type="variables_get" id="ngw$;lZg^;i!(FnUr6F=">
                            <field name="VAR">intVowelCount</field>
                          </block>
                        </value>
                        <value name="B">
                          <shadow type="math_number" id="[;j@V$]uo-%k%wH3OE4D">
                            <field name="NUM">1</field>
                          </shadow>
                          <block type="text_count" id="^BU61R`8R)r]]]swzqsd">
                            <value name="SUB">
                              <shadow type="text" id="WXI7vpRn{^S=[3XqP/-a">
                                <field name="TEXT">i</field>
                              </shadow>
                            </value>
                            <value name="TEXT">
                              <shadow type="text" id="}txQLs~Gq!/d8gKFE`Vn">
                                <field name="TEXT"></field>
                              </shadow>
                              <block type="variables_get" id="`ddn2^R(Pl*D?e?dKnfh">
                                <field name="VAR">strInputWord</field>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </value>
                    <next>
                      <block type="variables_set" id="|Bl5Hsf}kTVHTU^$_U55">
                        <field name="VAR">intVowelCount</field>
                        <value name="VALUE">
                          <block type="math_arithmetic" id="y}n{:C_RbP~5X2o3%mJE">
                            <field name="OP">ADD</field>
                            <value name="A">
                              <shadow type="math_number" id="3N|8|VA9^ltZMdK9:+8f">
                                <field name="NUM">1</field>
                              </shadow>
                              <block type="variables_get" id="?Qu{p-Tlu(lsCajzP=N5">
                                <field name="VAR">intVowelCount</field>
                              </block>
                            </value>
                            <value name="B">
                              <shadow type="math_number" id="[;j@V$]uo-%k%wH3OE4D">
                                <field name="NUM">1</field>
                              </shadow>
                              <block type="text_count" id="Xi5b_aqEx=BT|yHrN*z%">
                                <value name="SUB">
                                  <shadow type="text" id="~]rIKMq*/}**^01yGWje">
                                    <field name="TEXT">o</field>
                                  </shadow>
                                </value>
                                <value name="TEXT">
                                  <shadow type="text" id="}txQLs~Gq!/d8gKFE`Vn">
                                    <field name="TEXT"></field>
                                  </shadow>
                                  <block type="variables_get" id="4/8zgW0v(k)BfIoWfZJw">
                                    <field name="VAR">strInputWord</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                          </block>
                        </value>
                        <next>
                          <block type="variables_set" id="/6l$,Wz95SG%*fpGy2k^">
                            <field name="VAR">intVowelCount</field>
                            <value name="VALUE">
                              <block type="math_arithmetic" id="IzX)!-hW1opB+rbgpZDK">
                                <field name="OP">ADD</field>
                                <value name="A">
                                  <shadow type="math_number" id="3N|8|VA9^ltZMdK9:+8f">
                                    <field name="NUM">1</field>
                                  </shadow>
                                  <block type="variables_get" id="WmNk_?R!]fIHY.Rjyr=y">
                                    <field name="VAR">intVowelCount</field>
                                  </block>
                                </value>
                                <value name="B">
                                  <shadow type="math_number" id="[;j@V$]uo-%k%wH3OE4D">
                                    <field name="NUM">1</field>
                                  </shadow>
                                  <block type="text_count" id="1_J[9]+pH_fn8!VgGe7i">
                                    <value name="SUB">
                                      <shadow type="text" id="Ub1M6%-z$.m]L+;BFq~{">
                                        <field name="TEXT">u</field>
                                      </shadow>
                                    </value>
                                    <value name="TEXT">
                                      <shadow type="text" id="}txQLs~Gq!/d8gKFE`Vn">
                                        <field name="TEXT"></field>
                                      </shadow>
                                      <block type="variables_get" id="`~`=%485}hJ:+Hv.+pF:">
                                        <field name="VAR">strInputWord</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </value>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="Q!lZ$l_r:TGDK$3,y]KU">
        <field name="VAR">intVowelCount</field>
      </block>
    </value>
  </block>
  <block type="procedures_defreturn" id="}V`xa)5o,+3HNC0(]xXF" x="-688" y="38">
    <mutation>
      <arg name="strInputWord"></arg>
    </mutation>
    <field name="NAME">CountVowels2</field>
    <statement name="STACK">
      <block type="variables_set" id="ugSY2(E$59|;y{9kAzD0">
        <field name="VAR">intVowelCount2</field>
        <value name="VALUE">
          <block type="math_number" id="1_{C45?p5[=q$iX[7EU?">
            <field name="NUM">0</field>
          </block>
        </value>
        <next>
          <block type="variables_set" id="jxS{EFU]Q{9[!/U8XxS4">
            <field name="VAR">strInputWord</field>
            <value name="VALUE">
              <block type="text_changeCase" id="{WMF+aoFtSmjwB/$VAfk">
                <field name="CASE">LOWERCASE</field>
                <value name="TEXT">
                  <shadow type="text" id="If5CODK*2(Go0Oq43)%*">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="variables_get" id="o-TOP]|M4}@Q_.o.TLDt">
                    <field name="VAR">strInputWord</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="controls_for" id="wUcra3rOB?5Jczx:9{.x">
                <field name="VAR">i</field>
                <value name="FROM">
                  <shadow type="math_number" id=".)SC+_(#}Bj7H~5mTW$,">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <value name="TO">
                  <shadow type="math_number" id="(RC(oE1|]Yt9~S]Qi@.q">
                    <field name="NUM">10</field>
                  </shadow>
                  <block type="text_length" id="@]~@]X?Hk%+qx6J@3|ZT">
                    <value name="VALUE">
                      <shadow type="text" id=",dh*k3,8UTYC[@IZp#3N">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="variables_get" id="QPzrHse~vz]/)m|T9]|~">
                        <field name="VAR">strInputWord</field>
                      </block>
                    </value>
                  </block>
                </value>
                <value name="BY">
                  <shadow type="math_number" id="`Ss?PS5`8H,ibhjD,y(S">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <statement name="DO">
                  <block type="variables_set" id="1XL|kK=/qn8)}fVX|TB:">
                    <field name="VAR">strCurrentLetter</field>
                    <value name="VALUE">
                      <block type="text_charAt" id="76}H[v*^#v3T`Y]]bjHk">
                        <mutation at="true"></mutation>
                        <field name="WHERE">FROM_START</field>
                        <value name="VALUE">
                          <block type="variables_get" id="|l(w3v3d-iW,~;4W|cMb">
                            <field name="VAR">strInputWord</field>
                          </block>
                        </value>
                        <value name="AT">
                          <block type="variables_get" id="9$yWKk77r@nDMMS%yT_)">
                            <field name="VAR">i</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <next>
                      <block type="controls_if" id=".9xlL|1qa~[mJZ+)3o{w">
                        <value name="IF0">
                          <block type="logic_compare" id="8xMFc+Lq01#d`i$WaNbj">
                            <field name="OP">GT</field>
                            <value name="A">
                              <block type="text_indexOf" id="XkwxfMJq*43lk9Gt04am">
                                <field name="END">FIRST</field>
                                <value name="VALUE">
                                  <block type="text" id="$mKRt%MWVFymefhmh_F~">
                                    <field name="TEXT">aeiou</field>
                                  </block>
                                </value>
                                <value name="FIND">
                                  <shadow type="text" id="eE)85-8HYQ#U)fX;O12m">
                                    <field name="TEXT">abc</field>
                                  </shadow>
                                  <block type="variables_get" id="zoY^.}E)xHFNw,:OXQfB">
                                    <field name="VAR">strCurrentLetter</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="B">
                              <block type="math_number" id="xhy3;VX-fy*p|x8$cpWP">
                                <field name="NUM">0</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <statement name="DO0">
                          <block type="math_change" id="roy[L[?g(j_9B|=WM0oM">
                            <field name="VAR">intVowelCount2</field>
                            <value name="DELTA">
                              <shadow type="math_number" id="FOwzVd9`APk[F;?42wAD">
                                <field name="NUM">1</field>
                              </shadow>
                            </value>
                          </block>
                        </statement>
                      </block>
                    </next>
                  </block>
                </statement>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="kg$!^bfWj)!@LFQ4:]Cp">
        <field name="VAR">intVowelCount2</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="$jv=s=dC3D|ue;qz0v,5" x="-688" y="338">
    <field name="VAR">strUserWord</field>
    <value name="VALUE">
      <block type="text_prompt_ext" id="5]s?`}YP(giB$1fq:*O#">
        <mutation type="TEXT"></mutation>
        <field name="TYPE">TEXT</field>
        <value name="TEXT">
          <shadow type="text" id="Ug653*TY[W[hh97Afe.c">
            <field name="TEXT">Please enter word for vowel count</field>
          </shadow>
        </value>
      </block>
    </value>
    <next>
      <block type="text_print" id="ND_{fD1m#ER!oSuu%gHT">
        <value name="TEXT">
          <shadow type="text" id="2^=+O?e09E$ydh^HHs@N">
            <field name="TEXT">abc</field>
          </shadow>
          <block type="text_join" id="rF~:%)Z[rSa!eNP76s]M">
            <mutation items="6"></mutation>
            <value name="ADD0">
              <block type="text" id="L6eM%ke+|7$9aGMTL{:h">
                <field name="TEXT">Number of vowels in </field>
              </block>
            </value>
            <value name="ADD1">
              <block type="variables_get" id="V1{bb0J$U$!GUGB,fX;I">
                <field name="VAR">strUserWord</field>
              </block>
            </value>
            <value name="ADD2">
              <block type="text" id="*9r3xCV0z+_yB|:)5Dn|">
                <field name="TEXT"> by function CountVowels: </field>
              </block>
            </value>
            <value name="ADD3">
              <block type="procedures_callreturn" id=",pqVXc3X,P*%X`sXmh%r">
                <mutation name="CountVowels">
                  <arg name="strInputWord"></arg>
                </mutation>
                <value name="ARG0">
                  <block type="variables_get" id="%UyGSq0.:MbsbK7gr4nl">
                    <field name="VAR">strUserWord</field>
                  </block>
                </value>
              </block>
            </value>
            <value name="ADD4">
              <block type="text" id="$*Njf4F]uk4}rASWpQO1">
                <field name="TEXT"> and  by function CountVowels2: </field>
              </block>
            </value>
            <value name="ADD5">
              <block type="procedures_callreturn" id="EiS*H[k6i*YtdV+;RQLx">
                <mutation name="CountVowels2">
                  <arg name="strInputWord"></arg>
                </mutation>
                <value name="ARG0">
                  <block type="variables_get" id="}2A[=bC)YLE=tK9TOV)_">
                    <field name="VAR">strUserWord</field>
                  </block>
                </value>
              </block>
            </value>
          </block>
        </value>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var intVowelCount: Integer;
var intVowelCount2: Integer;
var strCurrentLetter: String;
var strInputWord: String;
var strUserWord: String;


function textCount(haystack, needle: string): integer;
begin
  if (length(needle) = 0) then
    result := length(haystack) + 1  else
    result := length(StrSplit(haystack, needle)) - 1;
end;


function CountVowels(strInputWord: String): Integer;
begin
  strInputWord := LowerCase(strInputWord);
  intVowelCount := textCount(strInputWord, 'a');
  intVowelCount := intVowelCount + (textCount(strInputWord, 'e'));
  intVowelCount := intVowelCount + (textCount(strInputWord, 'i'));
  intVowelCount := intVowelCount + (textCount(strInputWord, 'o'));
  intVowelCount := intVowelCount + (textCount(strInputWord, 'u'));
  Result := intVowelCount;
end;


function CountVowels2(strInputWord: String): Integer;
begin
  intVowelCount2 := 0;
  strInputWord := LowerCase(strInputWord);
  for var i := 1 to length(strInputWord) step 1 do
  begin
    strCurrentLetter := CharAt(strInputWord, (i - 1) + 1);
    if ((Pos(strCurrentLetter, 'aeiou') > 0)) then
    begin
      intVowelCount2 += 1;
    end;
  end;
  Result := intVowelCount2;
end;


function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;



strUserWord := prompt('Please enter word for vowel count');
Console.writeln(StrJoin([Str('Number of vowels in '), Str(strUserWord), Str(' by function CountVowels: '), Str(CountVowels(strUserWord)), Str(' and  by function CountVowels2: '), Str(CountVowels2(strUserWord))],''));
    

Generated JavaScript

var intVowelCount, strInputWord, intVowelCount2, strCurrentLetter, i, strUserWord;

function textCount(haystack, needle) {
  if (needle.length === 0) {
    return haystack.length + 1;
  } else {
    return haystack.split(needle).length - 1;
  }
}

function CountVowels(strInputWord) {
  strInputWord = strInputWord.toLowerCase();
  intVowelCount = textCount(strInputWord, 'a');
  intVowelCount = intVowelCount + (textCount(strInputWord, 'e'));
  intVowelCount = intVowelCount + (textCount(strInputWord, 'i'));
  intVowelCount = intVowelCount + (textCount(strInputWord, 'o'));
  intVowelCount = intVowelCount + (textCount(strInputWord, 'u'));
  return intVowelCount;
}

function CountVowels2(strInputWord) {
  intVowelCount2 = 0;
  strInputWord = strInputWord.toLowerCase();
  var i_end = strInputWord.length;
  var i_inc = 1;
  if (1 > i_end) {
    i_inc = -i_inc;
  }
  for (i = 1; i_inc >= 0 ? i <= i_end : i >= i_end; i += i_inc) {
    strCurrentLetter = strInputWord.charAt((i - 1));
    if ('aeiou'.indexOf(strCurrentLetter) + 1 > 0) {
      intVowelCount2 = (typeof intVowelCount2 == 'number' ? intVowelCount2 : 0) + 1;
    }
  }
  return intVowelCount2;
}


strUserWord = window.prompt('Please enter word for vowel count');
window.alert(['Number of vowels in ',strUserWord,' by function CountVowels: ',CountVowels(strUserWord),' and  by function CountVowels2: ',CountVowels2(strUserWord)].join(''));

Generated Python Code

from numbers import Number

intVowelCount = None
strInputWord = None
intVowelCount2 = None
strCurrentLetter = None
i = None
strUserWord = None

def CountVowels(strInputWord):
  global intVowelCount, intVowelCount2, strCurrentLetter, i, strUserWord
  strInputWord = strInputWord.lower()
  intVowelCount = strInputWord.count('a')
  intVowelCount = intVowelCount + strInputWord.count('e')
  intVowelCount = intVowelCount + strInputWord.count('i')
  intVowelCount = intVowelCount + strInputWord.count('o')
  intVowelCount = intVowelCount + strInputWord.count('u')
  return intVowelCount

def upRange(start, stop, step):
  while start <= stop:
    yield start
    start += abs(step)

def downRange(start, stop, step):
  while start >= stop:
    yield start
    start -= abs(step)

def CountVowels2(strInputWord):
  global intVowelCount, intVowelCount2, strCurrentLetter, i, strUserWord
  intVowelCount2 = 0
  strInputWord = strInputWord.lower()
  i_end = float(len(strInputWord))
  for i in (1 <= i_end) and upRange(1, i_end, 1) or downRange(1, i_end, 1):
    strCurrentLetter = strInputWord[int(i - 1)]
    if 'aeiou'.find(strCurrentLetter) + 1 > 0:
      intVowelCount2 = (intVowelCount2 if isinstance(intVowelCount2, Number) else 0) + 1
  return intVowelCount2

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


strUserWord = text_prompt('Please enter word for vowel count')
print(''.join([str(x) for x in ['Number of vowels in ', strUserWord, ' by function CountVowels: ', CountVowels(strUserWord), ' and  by function CountVowels2: ', CountVowels2(strUserWord)]]))

Copy of Output

Number of vowels in facetious by function CountVowels: 5
and by function CountVowels2: 5
Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion