Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Counting Number of Letters in a String (http://www.programmingforums.org/showthread.php?t=14168)

mattireland Oct 17th, 2007 3:44 PM

Counting Number of Letters in a String
 
Hi,

I'm trying to count the number of letters in a string. e.g. I want to know how many e's there would be in string1 or t's there would be in string2

I was going to display them in a datagrid but abandoned that idea and now want to display them as individual integers so how many e's there are would be something like intE.

Thanks!

mattireland

andro Oct 17th, 2007 4:08 PM

Re: Counting Number of Letters in a String
 
Iterate through the string character by character and count the occurrences of each letter :)

Infinite Recursion Oct 17th, 2007 4:35 PM

Re: Counting Number of Letters in a String
 
http://www.freevbcode.com/ShowCode.asp?ID=1025

?

Jabo Oct 20th, 2007 3:41 PM

Re: Counting Number of Letters in a String
 
irrelevant reply and no way to delete post any longer.

Booooze Oct 21st, 2007 1:02 AM

Re: Counting Number of Letters in a String
 
This is an easy way to do it in C#. I'm sure .NET has easier built in functions though. All you'll have to do is convert it over, but for the most part it's the same.

:

  1.             string str = "Testing";
  2.             char testchr = 't';
  3.             int number = 0;
  4.  
  5.             foreach (char chr in str)
  6.             {
  7.                 if (chr == testchr)
  8.                 {
  9.                     number++;
  10.                 } //End if
  11.             } //End foreach
  12.  
  13.             //Show number
  14.             MessageBox.Show(number.ToString());


mattireland Oct 27th, 2007 8:33 AM

Re: Counting Number of Letters in a String
 
Thanks very much guys! Especially to IR and Booze (IR I used that link and found loads of other good stuff on there - thanks! Booze thanks for that I used it in a C# variation that I was doing). I was waiting until I had the program finished before I replied to this thread so you could see what I've done with it but alas, I've not quite finished it yet.

Thanks again,

Matt.

knetcozd May 23rd, 2008 6:58 AM

Re: Counting Number of Letters in a String
 
just out of interest what is the purpose of this program?

mbd May 23rd, 2008 11:37 AM

Re: Counting Number of Letters in a String
 
of course .net has easier built in functions:
:

  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace RegExTest
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Regex regex = new Regex("t");
  11.             Console.Out.WriteLine(regex.Matches("this is a test").Count);
  12.         }
  13.     }
  14. }

sorry for the c#, but you can always use sharpdevelop to convert this to vb

mattireland May 23rd, 2008 2:32 PM

Re: Counting Number of Letters in a String
 
Thanks.

@ knetcozd: Cipher cracking.

Jimbo May 23rd, 2008 8:59 PM

Re: Counting Number of Letters in a String
 
Quote:

Originally Posted by mattireland (Post 145501)
Cipher cracking.

Assuming you're doing this by frequency analysis, you should probably opt for a method where you have an array of 26 ints (one per letter), or something along the lines of Dictionary(Of Char, Integer) so you can get the counts for all of the letters with a single pass.


All times are GMT -5. The time now is 3:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC