Friday 29 June 2012

Validate Words in C#


Text==>u r input Value,,,,,Lenth==>Lenth of words to be check    
 public int ValidateWords(string Text, int Length)
        {
            int result = 0;
            bool lastWasSpace = false;

            foreach (char c in Text)
            {
                if (char.IsWhiteSpace(c))
                {
                    if (lastWasSpace == false)
                    {
                        result++;
                    }
                    lastWasSpace = true;
                }
                else
                {
                    // Count other characters every time.
                    //result++;
                    lastWasSpace = false;
                }
            }

            if (result <= Length && result > 0)
            {
                return 1;
            }
            else if (result > Length)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }

1 comment: