word count and length

  • Thread starter Thread starter noname
  • Start date Start date
N

noname

Guest
Hi, this is my first post and for those who are wondering, I'm a newbie...so I shouldn't crack your head up at the beginning...

I am trying to develop a little application to count words and their length
I have tried several ways but haven't found an efficient way to do so...
I want to count the total amount of words in a textbox and also the amount of 1 character, 2 characters, 3 characters and +3 character words....
could anybody help me out on this!?
it would be greatly appreciated...
thanks!
 
Well, to split a string up into its separate words, you can use the
.Split() function of the string. You'll need to define a list of possible
word seperators as well, so something like this:

Visual Basic:
Dim seperatorList() As Char = _
  (".,?-" & ControlChars.CrLf).ToCharArray()
Dim wordList() As String
Dim wordsToSplit As String = "the quick brown fox jumps over the lazy dog"

'Splits the words up using all of the possible word seperators.
'You'll need to add to the seperator list yourself, because it's by
'no means a complete list, but suitable for demo purposes.
wordList = wordsToSplit.Split(seperatorList)
This will fill the wordList array with all of the words in the sentance
"the quick brown fox jumps over the lazy dog".

Now, you can loop through each string in the array and check the
lengths using the .Length() method.
Visual Basic:
Dim temp As String

For Each temp In wordList()
  'Use temp.Length to get the length of the word.
Next

To get the total number of words, use wordList.Length.
 
function to convert number to words

i want function which convert numeric value in to words. e.g. I pass 1234 as a parameter & the function return One Thousand Two Hundrerd Thirty Four.
 
I normally don't do code, but I did this on the other forum:
C#:
using System;
using System.Text;

public static class NumberToString
{
    static string[] ones = new string[] { "One ", "Two ", "Three ", "Four ", 
                                    "Five ", "Six ", "Seven ", "Eight ", "Nine " };

    static string[] teens = new string[] {"Ten ", "Eleven ", "Twelve ", "Thirteen ", 
                                    "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", 
                                    "Eighteen ", "Nineteen "};

    static string[] tens = {"Twenty ", "Thirty ", "Forty ", "Fifty ", 
                                   "Sixty ", "Seventy ", "Eighty ", "Ninety "};

    static string[] levels = { "Thousand ", "Million ", "Billion " };


    public static string Convert(int number)
    {
        if (number == 0)
        {
            return "Zero";
        }

        StringBuilder final = new StringBuilder();

        StringBuilder text = new StringBuilder();

        
        int level = 0;        

        while (number != 0)
        {            
            int temp = number / 1000;

            int current = number % 1000;

            number = temp;


            int one = current % 10;

            int ten = current % 100 / 10;

            int hundred = current / 100;

            if (hundred > 0)
            {
                text.Append(ones[hundred]);

                text.Append("Hundred ");
            }

            if (ten == 1)
            {
                text.Append(teens[one]);
            }
            else if (ten > 0)
            {
                text.Append(tens[ten - 2]);      
            }

            if (one > 0)
            {
                text.Append(ones[one - 1]);
            }

            if (level > 0)
            {
                text.Append(levels[level - 1]);               
            }

            final.Insert(0, text);

            text.Remove(0, text.Length);

            level++;
        }

        return final.ToString();
    }
}
 
Last edited:
Back
Top