Click here to Skip to main content
14,733,443 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Public Sub word()
            Dim l, i, number As Integer
            no = 0
            Console.WriteLine("Type the text")
            s = Console.ReadLine()
            l = Len(s)
            Do While (i < l)
                If s(i) = "" Then
                    no = no + 1

                End If
                i = i + 1
                number = no
            Loop

            Console.WriteLine("The no of words is {0}", number)
        End Sub
    End Class


What I have tried:

i have tried a lot of times .it says the no of words is 0.
Posted
Updated 7-Sep-16 10:36am
Comments
[no name] 7-Sep-16 10:03am
   
Maybe it's time you learned how to use the debugger instead of expecting us to teach you programming.
Member 12725211 7-Sep-16 10:05am
   
I like your honesty ..ok will try but a few suggestion doesn't hurt
Maciej Los 7-Sep-16 13:33pm
   
Define, what you mean by saying: "word". Does the word is punctuation sign like: {",", ";", "."} or {"Ms.", "Mr."}, etc.?

   
You should try:
If s(i) = " " Then

a space was added between quotes

Otherwise, you can simplify your code:
    Public Sub word()
        Dim l, i , number As Integer
        no = 0
        Console.WriteLine("Type the text")
        s = Console.ReadLine()
        l = Len(s)
        Do While (i < l)
            If s(i) = " " Then
                no = no + 1

            End If
            i = i + 1
            number = no
        Loop

        Console.WriteLine("The no of words is {0}", number no)
    End Sub
End Class
   
v3
Comments
Richard MacCutchan 7-Sep-16 11:37am
   
Can you be sure that i is zero at the beginning of the loop? (I am not a VB expert, as you may have guessed).
Patrice T 7-Sep-16 11:41am
   
Unfortunately, VB gives default values to variables and fo not enforce manual initialization.
Richard MacCutchan 7-Sep-16 13:12pm
   
:thumbsup:
Maciej Los 7-Sep-16 13:46pm
   
This is partially true. It depends on few factors. For further details, please see: Dim Statement (Visual Basic) (Default Data Types and Values - section)
Please, read my comment to the question first. As i mentioned there, it depends on what you define as a word.

There's a lot of similar questions[^] on this forum. For example:
How to split string in words[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900