Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question...can I use IComparer to compare an element in an array against another element in the same array??? A string array??
Or would I simply use a for loop and if statement to find and compare??

Here is what I am trying to do...I have put together a doubly linked list with an array. What comes out is a sentence. I need to count how many times a word takes place in that sentence and return that result.

Is that possible with IComparer?

Thanks
Posted

If it's a sentence, you shouldn't put the words into a doubly linked list

If you're counting occurrences of a word, I would probably just do something like this:

C#
public int CountWord(string sentence, string word, bool caseSensitive)
{
    int count = 0;
    string[] parts = sentence.Split(' ');
    if (!caseSensitive)
    {
        word = word.ToUpper();
    }
    foreach(string part in parts)
    {
        if (!caseSensitive)
        {
            part = part.ToUpper();
        }
        count += (part == word) ? 1 : 0;
    }
    return count;
}
 
Share this answer
 
v2
If it's a doubly-linked list, how do you know when to stop counting?
 
Share this answer
 
Comments
Sandeep Mewara 26-Nov-10 0:20am    
Comment from OP:
Thats a good question. I didnt think about that until just now. I guess with a counter of some sort.
Im just now tackling this and I have no idea where to start it off at. Stopping it would probably be the last thing I determine.

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