Jump to content
Xtreme .Net Talk

word count and length


Recommended Posts

Posted

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!

  • *Experts*
Posted

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:

 

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.

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.

  • 3 years later...
Posted

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.

Posted (edited)

I normally don't do code, but I did this on the other forum:

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();
   }
}

Edited by IceAzul

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...