Locates words in a given text with a minimum number of characters and prints those on the console. - CSharp Language Basics

CSharp examples for Language Basics:char

Description

Locates words in a given text with a minimum number of characters and prints those on the console.

Demo Code

using System;//  w ww .j a v  a  2 s. c o  m
public class WordExtractor
{
   public static void Main()
   {
      string myText;
      int minLength = 0;
      int wordBegin = 0;
      int wordEnd = 0;
      int wordLength = 0;
      int adjTextLength = 0;
      char ch;
      Console.WriteLine("Enter text:");
      myText = Console.ReadLine();
      Console.WriteLine("Enter minimum length" + " of words to be displayed");
      minLength = Convert.ToInt32(Console.ReadLine());
      adjTextLength = myText.Length - 1;
      while(wordEnd < adjTextLength)
      {
         wordEnd++;
         ch = myText[wordEnd];
         if(char.IsWhiteSpace(ch))
         {
            wordLength = wordEnd - wordBegin;
            if(wordLength >= minLength)
            {
               Console.WriteLine(myText.Substring(wordBegin, wordLength));
            }
            wordBegin = wordEnd + 1;
         }
      }
      wordLength = wordEnd - wordBegin + 1;
      if(wordLength >= minLength)
      {
         Console.WriteLine(myText.Substring(wordBegin, wordEnd - wordBegin + 1));
      }
   }
}

Result


Related Tutorials