Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi all,

I need to create a program that shows a sentence with the most words in it.

My 3 sentences are:

I like apples. I like red apples. I like red apples than green apples.

How do I show a sentence with the most words in it?
(The result should be displaying "I like red apples than green apples")

I'm new to programming so please understand that I am not able to post my code here because I don't even know where to begin....

Thanks a lot in advance~
Posted
Updated 6-Nov-14 20:33pm
v3

C#
// requires Linq

private string testText = @"I like apples. I like red apples. I like red apples than green apples.";

private char[] sentenceDelimiter = new char[] {'.'};

// slower: sort, O(nlogn)
private string GetLongestSort(string sentences)
{
    return
        sentences.Split(sentenceDelimiter)
            .OrderByDescending(s => s.Length).FirstOrDefault();
}

// much faster: aggregate, O(n)
private string GetLongestAggregate(string sentences)
{
    return
        sentences.Split(sentenceDelimiter)
            .Aggregate("", (lenVar, itm) => lenVar.Length > itm.Length ? lenVar: itm);
}

// test ... inside a method or EventHandler
string longest1 = GetLongestSort(testText);

string longest2 = GetLongestAggregate(testText);
 
Share this answer
 
v2
Comments
Member 10977819 7-Nov-14 2:24am    
Hi BillWoodruff! Thanks for the solution and the time you took to write the solution but unfortunately I have no idea about Linq.... but thanks anyway~
BillWoodruff 7-Nov-14 3:30am    
You're welcome. Learning to use Linq will really help you become a better .NET developer; if you've had experience with SQL query, you'll be on familiar ground.

Sacha Barber, one of CodeProject's smartest technical minds has written a three-part introduction to Linq, DLinq, and XLinq: may I suggest you read:

http://www.codeproject.com/Articles/18116/LINQ-Introduction-Part-Of

If you have access to one of Andrew Troelsen's .NET books (3.5 or later), I think his explanations/explorations of Linq are exceptionally clear:

http://www.amazon.com/s/ref=dp_byline_sr_book_1?ie=UTF8&field-author=Andrew+Troelsen&search-alias=books&text=Andrew+Troelsen&sort=relevancerank

cheers, Bill
Member 10977819 7-Nov-14 20:27pm    
Hi Bill, WoW! I'm so impressed that many people are actually willing to help out a stranger! So cheesy but I wanna say the world is beautiful~ haha :D and yes! I am okay(I guess) with SQL and T-SQL query but I was chickened out coz Linq seems totally different language(maybe it is a totally different language that I need to learn from scratch) :) Thanks for the links and I'll check it out soon!
BillWoodruff 8-Nov-14 1:25am    
We all start at the beginning, sometime :) The LinqPad free software, and its website, created by Joseph Albahari is a very valuable resource for learning Linq:

http://www.linqpad.net/
Maciej Los 7-Nov-14 3:23am    
I like Linq, +5!
Please try to get an idea from here, If you defecult to get output from here, I will write a sample code. But before try to find using this.

http://stackoverflow.com/questions/13485856/count-how-many-words-in-each-sentence[^]
 
Share this answer
 
Comments
Member 10977819 7-Nov-14 2:25am    
Hi, Gihan Liyanage! Thanks for the link and it was helpful!
Hi,

Please try the following code:
C#
public int GetNoOfWords(string s)
{
    return s.Split(new char[] { ' ', '.', ',','?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}


The above code assumes space, comma, full stop, question mark as word separators and will return the word count.

And call this function like this:
C#
string text = "This is an simple example which demonstrates how to count the number words in a string using C#. This is a test string.Hence, Test it properly in c#.";
int words = GetNoOfWords(text)


Check where you get the count to be maximum.

Hope this helps !!

Regards,
Praneet
 
Share this answer
 
Comments
Member 10977819 7-Nov-14 2:22am    
Hi Praneet! Thanks for the time you took to write the solution!
Here i have one solution and hope this will definitely match with your requirement.

Here is a method to get the maximum word containing sentence from a whole paraghraph if passed as parameter to it.

C#
private string GetSentenceWithMostWords(string strParagraph)
        {
            int iWordsCount = 0;
            string strSentenceWithMostWords = string.Empty;
            string[] arrAllSentences = strParagraph.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strSentence in arrAllSentences)
            {
                string[] arrWords = strSentence.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (iWordsCount < arrWords.Length)
                {
                    iWordsCount = arrWords.Length;
                    strSentenceWithMostWords = strSentence + ".";
                }
            }

            return strSentenceWithMostWords;
        }


Here is the form load code to call it and get the proper result to view :-

C#
private void SentenceWithMostWordsTestForm_Load(object sender, EventArgs e)
        {
            string strParagraph = "I like apples. I like red apples. I like red apples than green apples.";
            string strSentenceWithMostWords = GetSentenceWithMostWords(strParagraph);
        }


Hope this is helpful.
 
Share this answer
 
v2
Comments
Member 10977819 7-Nov-14 2:23am    
Hi SRS! Thanks for the time you took to write the solution and your kind explanation :)
SRS(The Coder) 7-Nov-14 2:33am    
Always Wl cm :-)

C#
string[] st = { "I like apples.", "I like red apples.", "I like red apples than green apples." };

                string result = st.OrderByDescending(s => s.Length).First();

                this.label1.Text = result;
 
Share this answer
 

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