Results 1 to 12 of 12

Thread: How to determing punctuation...

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    38

    How to determing punctuation...

    I am trying to
    -nput a sentence. determine if the last letter is punchtuation (ie. period, question mark, exclamation point). if not, display an appropriate message.

    -input a sentence. count the spaces and determine the number of words, in that sentence

    What if statement would be appropriate in this scenario and please be simple, thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: How to determing punctuation...

    looks like homework?

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    38

    Re: How to determing punctuation...

    vb Code:
    1. If txtName.Text = "," Then
    2.             MsgBox("Your last punctuation was" & txtName.Text & "bye")
    3.         End If
    4.         If txtName.Text = "." Then
    5.             MsgBox("Your last punctuation was" & txtName.Text & "bye")
    6.         End If
    7.         If txtName.Text = "!" Then
    8.             MsgBox("Your last punctuation was" & txtName.Text & "bye")
    9.         End If
    10.         If txtName.Text = "?" Then
    11.             MsgBox("Your last punctuation was" & txtName.Text & "bye")
    12.         End If
    13.  
    14.     End Sub
    kinda stuck here =/

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    38

    Re: How to determing punctuation...

    Quote Originally Posted by .paul. View Post
    looks like homework?
    Shhhhh ok Im so stuck i've been at it for over two hours now no help =/

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: How to determing punctuation...

    ok. if you want to test if a string ends with a character, you'd use the [string].endswith function

    to count spaces you'd split the string on " " & number of spaces would be your array length -1

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    38

    Re: How to determing punctuation...

    vb Code:
    1. If [name2].EndsWith(",") Then
    2.             MsgBox("Your last punctuation was" & name2)
    3.         End If
    4.         If [name2].EndsWith(".") Then
    5.             MsgBox("Your last punctuation was" & name2)
    6.         End If
    7.         If [name2].EndsWith("!") Then
    8.             MsgBox("Your last punctuation was" & name2)
    9.         End If
    10.         If [name2].EndsWith(";") Then
    11.             MsgBox("Your last punctuation was" & name2)
    12.         End If
    13.         If [name2].EndsWith("?") Then
    14.             MsgBox("Your last punctuation was" & name2)
    15.         End If
    16.         If [name2].EndsWith(":") Then
    17.             MsgBox("Your last punctuation was" & name2)
    18.         End If
    19.         If [name2].EndsWith(" ") Then
    20.             MsgBox("Please have the correct punctuation at the end.")

    Ok so now I have it like this how would I make it so then I can make it viewable in a message box ?

    Sorry for the dumb questions, kinda new D:

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    38

    Re: How to determing punctuation...

    I tried it like this, help please how can i make it better.

    vb Code:
    1. name3 = [name2].EndsWith("," Or "." Or "?" Or "!" Or ";" Or ":")
    2.  
    3.  
    4.         lblPreview1.Text = name3
    5.  
    6.  
    7.  
    8.  
    9.         If [name2].EndsWith(",") Then
    10.             MsgBox("Your last punctuation was" & name3)
    11.         End If
    12.         If [name2].EndsWith(".") Then
    13.             MsgBox("Your last punctuation was" & name3)
    14.         End If
    15.         If [name2].EndsWith("!") Then
    16.             MsgBox("Your last punctuation was" & name3)
    17.         End If
    18.         If [name2].EndsWith(";") Then
    19.             MsgBox("Your last punctuation was" & name3)
    20.         End If
    21.         If [name2].EndsWith("?") Then
    22.             MsgBox("Your last punctuation was" & name3)
    23.         End If
    24.         If [name2].EndsWith(":") Then
    25.             MsgBox("Your last punctuation was" & name3)
    26.         End If
    27.         If [name2].EndsWith(" ") Then
    28.             MsgBox("Please have the correct punctuation at the end.")
    29.         End If
    30.  
    31.     End Sub

  8. #8
    Member
    Join Date
    Mar 2011
    Posts
    55

    Re: How to determing punctuation...

    Code:
            Dim lastpunc As String = "Your last punctuation was "
            With TextBox1.Text
                If .EndsWith(".") Then
                    MessageBox.Show(lastpunc & ".")
                ElseIf .EndsWith("!") Then
                    MessageBox.Show(lastpunc & "!")
                ElseIf .EndsWith("?") Then
                    MessageBox.Show(lastpunc & "?")
                Else
                    MessageBox.Show("No punctuation detected.")
                End If
            End With
    
            'Count words
            Dim text() As String = TextBox1.Text.Split(CChar(" "))
            Dim count As Integer = text.GetUpperBound(0) + 1
            MessageBox.Show("Word count: " & count)

    To answer the question you created a new thread for, it is Text.EndsWith(".") Or Text.EndsWith("!"), etc.

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: How to determing punctuation...

    By obtaining the last char in a string we can determine if the char is a punctuation character by using Char.IsPunctuation.

    Code:
    Dim Sentences As String() = {"Hello John", "Hello Mary!", "Hello Bob.", "Are you feeling good?"}
    For Each Sentence In Sentences
        If Sentence.EndsWithPunctuation Then
            Console.WriteLine("Your sentence '{0}' ends with punctuation", Sentence)
        Else
            Console.WriteLine("Your sentence '{0}' does not ends with punctuation", Sentence)
        End If
    Next
    Place in code module
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Function EndsWithPunctuation(ByVal sender As String) As Boolean
        Return Char.IsPunctuation(CChar(sender.Substring(sender.Length - 1, 1)))
    End Function

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to determing punctuation...

    Quote Originally Posted by kevininstructor
    Code:
    CChar(sender.Substring(sender.Length - 1, 1))
    Tut, tut!
    Code:
    sender(sender.Length - 1)
    or
    Code:
    sender.Last()
    Both assume that there is at least one character in the String, as does yours. This:
    Code:
    sender.LastOrDefault()
    doesn't even do that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    38

    Re: How to determing punctuation...

    Thanks

  12. #12
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: How to determing punctuation...

    Quote Originally Posted by jmcilhinney View Post
    Tut, tut!
    Code:
    sender(sender.Length - 1)
    or
    Code:
    sender.Last()
    Both assume that there is at least one character in the String, as does yours. This:
    Code:
    sender.LastOrDefault()
    doesn't even do that.
    Not sure if this deserves a "Tut Tut" as I realize that sender.LastOrDefault() is better (thanks for bringing this up) and should have expressed that there might be a possibility of say an empty string, I am so used to first checking for empty string in such a situation prior to using such a function as EndsWithPunctuation i.e.

    So this is one alternate way to work this out
    Code:
    Dim Sentences As String() = _
        { _
            "Hello John", _
            "Hello Mary!", _
            Nothing, _
            "Hello Bob.", _
            "", _
            "?", _
            "Are you feeling good?" _
        }
    
    For Each Sentence In Sentences
        If Sentence.HasValue Then
            If Sentence.EndsWithPunctuation Then
                Console.WriteLine("Your sentence '{0}' ends with punctuation", _
                                  Sentence)
            Else
                Console.WriteLine("Your sentence '{0}' does not ends with punctuation", _
                                  Sentence)
            End If
        Else
            Console.WriteLine("Sentence had no content")
        End If
    Next
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Function HasValue(ByVal sender As String) As Boolean
        Return Not String.IsNullOrEmpty(sender)
    End Function
    Any ways so if using assertion within the extension we would use
    Code:
    Dim Sentences As String() = _
        { _
            "Hello John", _
            "Hello Mary!", _
            Nothing, _
            "Hello Bob.", _
            "", _
            "?", _
            "Are you feeling good?" _
        }
    For Each Sentence In Sentences
        If Sentence.EndsWithPunctuation Then
            Console.WriteLine("Your sentence '{0}' ends with punctuation", _
                              Sentence)
        Else
            Console.WriteLine("Your sentence '{0}' does not ends with punctuation", _
                              Sentence)
        End If
    Next
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Function EndsWithPunctuation(ByVal sender As String) As Boolean
        Return If(Not String.IsNullOrEmpty(sender), _
                  Char.IsPunctuation(sender.LastOrDefault), False)
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width