Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 17th, 2007, 2:44 PM   #1
mattireland
Hobbyist Programmer
 
mattireland's Avatar
 
Join Date: Jul 2007
Location: Wales, United Kingdom
Posts: 199
Rep Power: 2 mattireland is on a distinguished road
Send a message via MSN to mattireland Send a message via Skype™ to mattireland
Unhappy 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
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old Oct 17th, 2007, 3:08 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Re: Counting Number of Letters in a String

Iterate through the string character by character and count the occurrences of each letter
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Oct 17th, 2007, 3:35 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Re: Counting Number of Letters in a String

http://www.freevbcode.com/ShowCode.asp?ID=1025

?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Oct 20th, 2007, 2:41 PM   #4
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 254
Rep Power: 1 Jabo is on a distinguished road
Re: Counting Number of Letters in a String

irrelevant reply and no way to delete post any longer.
Jabo is offline   Reply With Quote
Old Oct 21st, 2007, 12:02 AM   #5
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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.

csharp Syntax (Toggle Plain Text)
  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());
Booooze is offline   Reply With Quote
Old Oct 27th, 2007, 7:33 AM   #6
mattireland
Hobbyist Programmer
 
mattireland's Avatar
 
Join Date: Jul 2007
Location: Wales, United Kingdom
Posts: 199
Rep Power: 2 mattireland is on a distinguished road
Send a message via MSN to mattireland Send a message via Skype™ to mattireland
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.
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old May 23rd, 2008, 5:58 AM   #7
knetcozd
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0 knetcozd is on a distinguished road
Thumbs up Re: Counting Number of Letters in a String

just out of interest what is the purpose of this program?

Last edited by knetcozd; May 23rd, 2008 at 6:10 AM.
knetcozd is offline   Reply With Quote
Old May 23rd, 2008, 10:37 AM   #8
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Counting Number of Letters in a String

of course .net has easier built in functions:
c# Syntax (Toggle Plain Text)
  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
mbd is offline   Reply With Quote
Old May 23rd, 2008, 1:32 PM   #9
mattireland
Hobbyist Programmer
 
mattireland's Avatar
 
Join Date: Jul 2007
Location: Wales, United Kingdom
Posts: 199
Rep Power: 2 mattireland is on a distinguished road
Send a message via MSN to mattireland Send a message via Skype™ to mattireland
Re: Counting Number of Letters in a String

Thanks.

@ knetcozd: Cipher cracking.
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old May 23rd, 2008, 7:59 PM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3 Jimbo is on a distinguished road
Re: Counting Number of Letters in a String

Quote:
Originally Posted by mattireland View Post
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 1:01 PM
Throwing an exception when using string constructor csrocker101 C# 3 Apr 8th, 2007 2:04 PM
Counting a single character in a string tbohon Perl 3 Jul 13th, 2006 9:23 AM
finding the number of spaces in a String squishiful Java 6 Jul 6th, 2005 5:43 PM
Counting Number of Leaf Nodes Recursively(Binary Search Tree) BaroN NighT C++ 7 Apr 26th, 2005 9:28 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:29 AM.

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