?
Solved

Break up a sentence into words

Posted on 2004-11-18
11
Medium Priority
?
1,148 Views
Last Modified: 2008-02-01
A user enters a sentence and I need to break up each word. I know how to convert the sentence to CAPS. But I need to break it up and then compare the word with a word in an array. How would I do this. I've been working on this for weeks!

Thanks

I tried something like this
   If sentence.Substring(m, n) = phrase Then
            strand &= word(i).FrenchWord & " "
            m = n + 1
            End If

Phrase=user input word
0
Comment
Question by:grundy
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 2000 total points
ID: 12615348
How about this...

string mySentence = "foo bar baz";
string[] myWords = mySentence.Split( ' ' );

now each word in the sentence is an element int he string array.

Then you can loop through the words like so..

for ( int i = 0; i < myWords.Length; i++ ) {
   if ( myWords[i].ToUpper().Equals( phrase.ToUpper ) ) {
      //DO something
   }
}
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12615527
Hi,

What programming language do you use? I can help you in Java and Visual Basic.

Dave
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12615652
Woops, sorry thought I was looking at the C# language... now that I look at this closer this looks more like C or C++ and mine is rusty at the moment...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:grundy
ID: 12615663
This is VB.net
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12615692
Ah... cool then my suggestion should work.
0
 

Author Comment

by:grundy
ID: 12615711
NipNFriar,

How would I compare to an array of words that are in alphabetical order. The words are stored in an array as follows:

  Dim sr As IO.StreamReader = IO.File.OpenText("English, French, German.txt")
        Do While (sr.Peek  -1)
            i += 1
            word(i).EnglishWord = sr.ReadLine
            word(i).FrenchWord = sr.ReadLine
            word(i).GermanWord = sr.ReadLine
        Loop
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12615723
Hi,

8. Creating arrays with the Split(split-character) function.
This function allows you to create a one-dimensional array, by splitting a string by recognizing a certain character, then putting any text after the character on a new line in the array.
This code will pop up a message box For each item in the array, which is 4. Note that the first line is infact 0.

    Dim i As String = "Line 0|Line 1|Line 2|Line 3"
    Dim a() As String
    Dim j As Integer
    a = i.Split("|")
    For j = 0 To a.GetUpperBound(0)
    MsgBox(a(j))
    Next
   
Another use of this function could be for getting all the lines from a multiline text box as follows: This will pull all lines of the text box, and use them to create an array, which is stored in r. You extract these values from the array by selecting where in the array you wish to look. The look-in-line is defined after the r, in brackets. Example: Msgbox r(3) would pull the FORTH line of the array that is being held in r. Msgbox r(5) would pull the 6th line being held in the array.

    Dim a() As String
    Dim j As Integer
    a = TextBox1.Text.Split(Lf)
   
    For j = 0 To a.GetUpperBound(0)
    MsgBox(a(j))
    Next


I get that from
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=169&lngWId=10

I hope that helps.

Regards
Dave
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12615848
Hmmm.... If word is a collection or dictionary then you could use the contains method... is it possible to use that instead of an array?
0
 
LVL 3

Expert Comment

by:vippx
ID: 12616277
Hi,

If you need advanced string processing, splittling various kinds of strings into sub strings and a uniform metod of accessing it, then use of a concept of tokenizer offers a lot of power and flexibility. This StringTokenizer is a class implemented in Java by default, however for C# and also for understanding the concept better, here is code that provides a tokenizer class with its source and explanation on using it

http://www.codeproject.com/csharp/StringTokenizer.asp
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question