none
How do I count all the vowels & consonants in a phrase in Small Basic RRS feed

  • Question

  • Hi,

    How do I count the number of vowels and & consonants in a phrase?

    I've got something like this so far:

    Would I need to do this for every vowel? Also the lines don't seem to work

    For i = 1 To Text.GetLength(InputString)

      If Text.IsSubText(InputString, txtVa) then
        NVowels = NVowels + 1
      EndIf
      EndFor 

    txtVa ="a"
    TextWindow.WriteLine("Enter a phrase")
    InputString= TextWindow.Read()
    For i = 1 To Text.GetLength(InputString)
      If Text.IsSubText(InputString, txtVa) then
        NVowels = NVowels + 1
      EndIf
      EndFor 
      TextWindow.WriteLine("Number of vowels are: " + NVowels)

    It's difficult as you can't declare variables in SB.

    Thanks


    Wendy

    Saturday, March 24, 2012 6:00 AM

Answers

  • Hello paz67!

    You have to test all of the 5 vowels, that is "a", "e", "i", "o" and "u". In your code u only test for "a"?
    It's true there's no variable declaration at SB, 'cause all variables are strings which can be evaluated as numbers when appropriate. But I don't see why is that a problem in your code. @_@
    And I don't think Text.IsSubText() can count # of vowels. It just says weather there's at least 1 there, not how many!
    Here, I've re-written your code, hope you like it:

    TextWindow.WriteLine( "Enter a phrase:" )
    ' Convert all to low case, so we don't need to worry about cases anymore! '
    String = Text.ConvertToLowerCase( TextWindow.Read() )
    Len = Text.GetLength( String )
    
    For Pos = 1 To Len
      Char  = Text.GetSubText( String  Pos  1 )
      Ascii = Text.GetCharacterCode( Char )
      
      If Char = "a" Or Char = "e" Or Char = "i" Or Char = "o" Or Char = "u" Then
        NVowels = NVowels + 1
      ElseIf Ascii >= 98 And Ascii <= 122 Then    ' "b" = 98  &  "z" = 122 '
        NConsonants= NConsonants + 1
      EndIf
    EndFor
    
    TextWindow.WriteLine("Number of vowels are: " + NVowels)
    TextWindow.WriteLine("Number of consonants are: " + NConsonants)

    Saturday, March 24, 2012 9:32 AM
    Answerer
  • txtVa ="a"
    TextWindow.WriteLine("Enter a phrase")
    InputString= TextWindow.Read()
    NVowels=0
    For i = 1 To Text.GetLength(InputString)
      If Text.GetSubText(InputString,i,1)= txtVa Then
       NVowels = NVowels + 1
      EndIf
    Endfor 

    TextWindow.WriteLine("Number of vowels are: " + NVowels)

    Saturday, March 24, 2012 9:16 AM
    Answerer

All replies

  • txtVa ="a"
    TextWindow.WriteLine("Enter a phrase")
    InputString= TextWindow.Read()
    NVowels=0
    For i = 1 To Text.GetLength(InputString)
      If Text.GetSubText(InputString,i,1)= txtVa Then
       NVowels = NVowels + 1
      EndIf
    Endfor 

    TextWindow.WriteLine("Number of vowels are: " + NVowels)

    Saturday, March 24, 2012 9:16 AM
    Answerer
  • Hello paz67!

    You have to test all of the 5 vowels, that is "a", "e", "i", "o" and "u". In your code u only test for "a"?
    It's true there's no variable declaration at SB, 'cause all variables are strings which can be evaluated as numbers when appropriate. But I don't see why is that a problem in your code. @_@
    And I don't think Text.IsSubText() can count # of vowels. It just says weather there's at least 1 there, not how many!
    Here, I've re-written your code, hope you like it:

    TextWindow.WriteLine( "Enter a phrase:" )
    ' Convert all to low case, so we don't need to worry about cases anymore! '
    String = Text.ConvertToLowerCase( TextWindow.Read() )
    Len = Text.GetLength( String )
    
    For Pos = 1 To Len
      Char  = Text.GetSubText( String  Pos  1 )
      Ascii = Text.GetCharacterCode( Char )
      
      If Char = "a" Or Char = "e" Or Char = "i" Or Char = "o" Or Char = "u" Then
        NVowels = NVowels + 1
      ElseIf Ascii >= 98 And Ascii <= 122 Then    ' "b" = 98  &  "z" = 122 '
        NConsonants= NConsonants + 1
      EndIf
    EndFor
    
    TextWindow.WriteLine("Number of vowels are: " + NVowels)
    TextWindow.WriteLine("Number of consonants are: " + NConsonants)

    Saturday, March 24, 2012 9:32 AM
    Answerer
  • Thanks, works amazing!
    Friday, March 25, 2016 3:41 PM