viktor80 viktor80 - 2 months ago 11
C# Question

How to find special chars in string in each separate word characters order by number

How to get on output:


  1. Number of all characters in each word separately.

  2. Found character from findChar list and its ID by order from first character in each separate word (not from beginning of phrase).

  3. Number of all words in string.

  4. Number of all characters characters in string.



if I got list of characters to find:

var findChar = new List<string> {"o", "s", "f"};


and If string is:

string input = "You cannot successfully determine beforehand which side of the bread to butter";


Desired output should be:

Word 1 contains 3 chars where 2 is “o”.
Word 2 contains 6 chars where 5 is “o”.
Word 3 contains 12 chars where 1 is “s”, 6 is “s”, 7 is “s”, 8 is “f”.
Word 4 contains 9 chars.
Word 5 contains 10 chars where 3 is “f”, 4 is “o”.
Word 6 contains 5 chars.
Word 7 contains 4 chars where 1 is “s”.
Word 8 contains 2 chars where 1 is “o”, 2 is “f”.
Word 9 contains 3 chars.
Word 10 contains 5 chars.
Word 11 contains 2 chars where 2 is “o”.
Word 12 contains 6 chars.
Phrase contains 12 words, 67 characters.

Answer

Here is one way of doing this. I give a brief explanation of what each method is doing and also included some comments. Note that I have not included any error handling.

FindCharacters

private static List<string> FindCharacters(List<string> characters, string sentence)
{
    var output = new List<String>();
    var wordIndex = 0;

    // Iterate around each word
    foreach (var word in sentence.Split(' '))
    {
        wordIndex++;
        var tupleList = new List<Tuple<char, int>>();

        // Iterate around each character
        foreach (var character in characters)
        {
            // To get the index of the character it needs to be converted to an actual char
            var parsedChar = char.Parse(character);
            for (int i = word.IndexOf(parsedChar); i > -1; i = word.IndexOf(parsedChar, i + 1))
            {
                tupleList.Add(new Tuple<char, int>(parsedChar, i + 1));
            }
        }

        // Build the output line for this word and add it to the list
        output.Add(BuildLine(tupleList, wordIndex, word.Length));   
    }
    return output;
}

This method takes the input and creates a List<string> containing each line of output required. It does so by iterating around each word in the sentence and then around each character, saving the character index information as a List<Tuple<char, int>>. This is then passed to the BuildLine method to generate the line of output for that word.

BuildLine

private static string BuildLine(List<Tuple<char, int>> tupleList, int wordIndex, int wordLength)
{
    var output = $"Word {wordIndex} contains {wordLength} chars";

    for (int i = 0; i < tupleList.Count; i++)
    {
        if (i == 0)
        {
            output += $" where ";
        }
        else if (i != tupleList.Count)
        {
            output += ", ";
        }

        output += $"{tupleList[i].Item2} is \"{tupleList[i].Item1}\"";
    }

    output += ".";
    return output;
}

Here we are just using the tupleList we created along with wordLength and wordIndex to output the line for that word. There is also a check for the first and last words so that we know whether to include "where" or ",".

Usage

private static void Main()
{
    var findChar = new List<string> { "o", "s", "f" };
    string input = "You cannot successfully determine beforehand which side of the bread to butter";

    // Get the lines of output
    var outputLines = FindCharacters(findChar, input);

    // Write the lines
    foreach (var line in outputLines)
    {
        Console.WriteLine(line);
    }

    // Write the final line
    var wordCount = input.Split(' ').Count();
    input = input.Replace(" ", string.Empty);
    Console.WriteLine($"Phrase contains {wordCount} words, {input.Count()} characters.");
}

Make sure to remove all the spaces from the input string before counting the characters!

Output

Word 1 contains 3 chars where 2 is “o”.
Word 2 contains 6 chars where 5 is “o”.
Word 3 contains 12 chars where 1 is “s”, 6 is “s”, 7 is “s”, 8 is “f”. 
Word 4 contains 9 chars.
Word 5 contains 10 chars where 3 is “f”, 4 is “o”.
Word 6 contains 5 chars.
Word 7 contains 4 chars where 1 is “s”.
Word 8 contains 2 chars where 1 is “o”, 2 is “f”.
Word 9 contains 3 chars.
Word 10 contains 5 chars.
Word 11 contains 2 chars where 2 is “o”.
Word 12 contains 6 chars.
Phrase contains 12 words, 67 characters.