How to Count Words in a String in C#

This is very interesting and logic building program in C#. The program will count the number of words in a given string e.g. "Hello World" has 2 words.

Source Code 

using System;
using System.Collections.Generic;
using System.Text;

namespace assad
{
class countWords
{
// /*
static void Main(string[] args)
{
welcomeScrean();
String str = "quick brown fox jumps over the lazy dog";
string[] arr = str.Split(' '); // split the sentence in an array of words
Console.WriteLine("\tWords List:");
Console.WriteLine("");
for ( int i = 0 ; i < arr.Length ; i++ ) {
Console.WriteLine("\t"+arr[i]);
}
Console.WriteLine("");
Console.WriteLine("\tWords Count: ");
Console.WriteLine("\t" + arr.Length); // displays the counts
int ab = Console.Read();
}
private static void welcomeScrean()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("\t*******************************************************" + "\n"
+ "\t*******************************************************" + "\n"
+ "\t** Count **" + "\n"
+ "\t** Words in String **" + "\n"
+ "\t** Program **" + "\n"
+ "\t*******************************************************" + "\n"
+ "\t*******************************************************\n\n\n\n");
}
// */
}
}

Output of the Program

Count Words in a String in C#

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment