Question: I just want to find a special sub-string in another string and save it as another string. Here is the code:

```
char sub[13]={};
char *ptr = sub;
char src[100] = "SOME DATE HERE CBC: 2345,23, SOME OTHER DATA";
//                              |----------|
ptr = strstr(src,"CBC:");
strncpy(sub,ptr,sizeof(sub)-1);

```

Is this an efficient way or does a better method exist for this?
Thanks.

Answer: 
Order by `Rank` but then add another sorting, that says if the first letter is equal to the first letter of `spokenWord` mark as 0 otherwise 1.

```
matches.OrderBy(x => x.Rank).ThenBy(x=>x.Name[0] == spokenWord[0] ? 0 : 1)

```

so, in the same rank, all matches with 0 (first letter equal) will be first and then the rest
The other answers give how to order by rank followed by first letter of match, however that would not cover a scenario where in your example the search results had both: accounts and accrual? Then by just matching on first word we may still end up having a wrong sort order.

This points us to the direction where we should try to have another rank based on substring match of the search word to the results. I think this should actually be done in the server side function which is giving you the ranks in the serach result.

However a simple implementation of the same would be:

```
static void Main(string[] args)
{
    string spokenWord = "accounts";

    HashSet<string> search = new HashSet<string>(); //tokenize the search keywords and store in dictionary...

    for (int z = 0; z < 1; z++)
    {
        for (int inner = 1; inner <= spokenWord.Length - z; inner++)
        {
            // by default orders in order of increasing length of substring
            search.Add(spokenWord.Substring(z, inner));
        }
    }
    // for loop above wrtten usnig linq....however in this case I think for loops wins becuase of greater clarity
    //var items = Enumerable.Range(0, 1).SelectMany(x => Enumerable.Range(1, spokenWord.Length - x).Select(j => spokenWord.Substring(x, j)));

    List<Match> matches = new List<Match>();

    matches.Add(new Match { Rank = 1, Name = "dev" });
    matches.Add(new Match { Rank = 1, Name = "finance" });
    matches.Add(new Match { Rank = 1, Name = "accounts" });
    matches.Add(new Match { Rank = 1, Name = "abcounts" });

    // ordering using rank and new searchrank score
    foreach (var match in matches.Select(m => new { Name = m.Name, Rank = m.Rank, SearchRank = MatchRank(search, m.Name) }).OrderBy(x => x.Rank).ThenByDescending(x => x.SearchRank))
    {
        Console.WriteLine(String.Format("Name: {0}, Rank: {1}", match.Name, match.Rank));
    }
}

```

And the `MatchRank` function would be:

```
public static int MatchRank (HashSet<string> searchString, string value)
{
    int result = 0;
    foreach (var item in searchString.OrderByDescending(x=>x.Length))
    {                
        if (value.StartsWith(item, StringComparison.Ordinal))
        {
            result = item.Length;
            break;
        }
        result = 0; // no match
    }
    return result;
}

```

Please do note that this is just a simple implementation, might need to be optimized for performance.