w3resource

C#: Calculate the average word length in a string

C# Sharp Regular Expression: Exercise-2 with Solution

Write a C# Sharp program to calculate the average word length in a given string. Round the average length up to two decimal places.

Note: Ignore punctuation.

Sample Data:
("CPP Exercises." -> 6
("C# syntax is highly expressive, yet it is also simple and easy to learn.") -> 4
(“C# is an elegant and type-safe object-oriented language") -> 5.62

Sample Solution-1:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string text = "CPP Exercises.";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("The average word length in the said string: " + test(text));
            text = "C# syntax is highly expressive, yet it is also simple and easy to learn.";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("The average word length in the said string: " + test(text));
            text = "C# is an elegant and type-safe object-oriented language";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("The average word length in the said string: " + test(text));
        }

        public static double test(string text)
        {
            string new_text = Regex.Replace(text, "[^A-Za-z ]", "");
            double average_len = new_text.Split(' ').Select(x => x.Length).Average();
            return Math.Round(average_len, 2);
        }
    }
}

Sample Output:

Original string: CPP Exercises.
The average word length in the said string: 6
Original string: C# syntax is highly expressive, yet it is also simple and easy to learn.
The average word length in the said string: 4
Original string: C# is an elegant and type-safe object-oriented language
The average word length in the said string: 5.62

Flowchart:

Flowchart: C# Sharp Exercises - Calculate the average word length in a string

Sample Solution-2:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string text = "CPP Exercises.";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("The average word length in the said string: " + test(text));
            text = "C# syntax is highly expressive, yet it is also simple and easy to learn.";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("The average word length in the said string: " + test(text));
            text = "C# is an elegant and type-safe object-oriented language";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("The average word length in the said string: " + test(text));
        }

        public static double test(string text)
        {
           return Math.Round((double)text.Where(ch => char.IsLetter(ch)).Count() / (double)(text.Where(ch => char.IsWhiteSpace(ch)).Count() + 1), 2);
        }
    }
}

Sample Output:

Original string: CPP Exercises.
The average word length in the said string: 6
Original string: C# syntax is highly expressive, yet it is also simple and easy to learn.
The average word length in the said string: 4
Original string: C# is an elegant and type-safe object-oriented language
The average word length in the said string: 5.62

Flowchart:

Flowchart: C# Sharp Exercises - Check a string is Valid Hex Code or not

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Check a string is Valid Hex Code or not.
Next C# Sharp Exercise: Possible Palindromes from string of characters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.