Solved

picking different combination of words from an array of words

Posted on 2011-09-21
4
158 Views
Last Modified: 2012-05-12
I have a string for example : apple pink lotus baby pen paper red

i wan extract     6 words then 5 words 4 words 3 words 2 words then 1 word from this array.. for example: apple pink lotus baby pen paper
apple pink lotus baby pen red
apple pink lotus baby paper red

and so on....
apple pink lotus baby pen
...

apple
pink
lotus
baby pen

Please help thanks. If there is code in VB.net that will be great
0
Comment
Question by:TrialUser
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36578344
I've implemented to get 6 random strings. Likewise write for other cases. Refer: http://www.techrepublic.com/article/working-with-random-numbers-in-net-applications/5663283
using System;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;

namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            

            string[] myStrings = new string[7];
            myStrings = "apple pink lotus baby pen paper red".Split(" ".ToCharArray());


            Random objRandom = new Random();
            StringCollection objCollection = new StringCollection();

            //To get 6 values randomly
            for (int i = 0; i < 6; )
            {
                int random = objRandom.Next(0, 7); 

                if (!objCollection.Contains(myStrings[random]))
                {
                    objCollection.Add(myStrings[random]);
                    i++;
                }

            }

            foreach(string s in objCollection)
              Console.WriteLine(s);

            Console.Read();

        }
    }
}

Open in new window

0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36579474
here is a VB.NET function that will return the number of items that you require from a given string.
    Function getRandon(ByVal items As String, ByVal number As Integer) As String

        Dim objRandom As New Random
        Dim objCollection As New List(Of String)
        Dim objReturn As New List(Of String)
        Array.ForEach(items.Split(","), Sub(x) objCollection.Add(x))
        While objReturn.Count < number
            Dim random = objRandom.Next(0, number + 1)
            If Not objReturn.Contains(objCollection.ToList(random)) Then
                objReturn.Add(objCollection.ToList(random))
            End If
        End While
        Return String.Join(",", objReturn)
    End Function

Open in new window

call the function by:
Dim MyResult = getRandon(String.Join(",", {"apple", "pink", "lotus", "baby", "pen", "paper"}), 2)

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 500 total points
ID: 36580179
For results into a blank dictonary, do
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String))
For Each x In theList
    If Result.ContainsKey(x.Length) Then
        Result.Item(x.Length).Add(x)
    Else
        Result.Item(x.Length) = New List(Of String) From {x}
    End If
Next

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582262
You have asked the same question 3 times now!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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