Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 17th, 2009, 12:42 PM
asifsomy asifsomy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 31 asifsomy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 45 m 24 sec
Reputation Power: 1
Count common characters in two strings

is there any function in Visual Basic 2008 that returns the number of common characters in two strings.
for example:
string 1: "abcdef"
string 2: "abc"

the function should return 3.

Reply With Quote
  #2  
Old October 17th, 2009, 02:15 PM
requinix's Avatar
requinix requinix is offline
nuts - top 5 poster again :(
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 5,386 requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)requinix User rank is General 32nd Grade (Above 100000 Reputation Level)  Folding Points: 161825 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161825 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161825 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161825 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161825 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161825 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 1 Day 1 h 57 m 5 sec
Reputation Power: 3078
Send a message via MSN to requinix
Be more specific. Are the common characters just at the beginning of the string, or can they be scattered throughout?

What should the function return when given "abcdefg" and "abcefdg"?
__________________
How to ask a PHP question | My stuff

Reply With Quote
  #3  
Old October 17th, 2009, 02:26 PM
asifsomy asifsomy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 31 asifsomy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 45 m 24 sec
Reputation Power: 1
scatterd...
shuld return 7 in ur case.

Quote:
Originally Posted by requinix
Be more specific. Are the common characters just at the beginning of the string, or can they be scattered throughout?

What should the function return when given "abcdefg" and "abcefdg"?

Reply With Quote
  #4  
Old October 17th, 2009, 06:56 PM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 15th Plane (12000 - 12499 posts)
 
Join Date: Jun 2003
Posts: 12,265 Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level)Doug G User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 10 h 10 m 34 sec
Reputation Power: 2009
Something like:
Code:
For i = 1 to len(str1)
  c = mid(str1,i,1)
  if instr(str2, c) then
    count = count + 1
  end if
Next

You may need to verify the syntax of the functions, I don't do much vb these days.
__________________
======
Doug G
======
I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain

Reply With Quote
  #5  
Old October 19th, 2009, 02:16 PM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Click here for more information.
 
Join Date: Apr 2004
Location: Oakland, CA
Posts: 13,970 medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)medialint User rank is General 70th Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 3 Weeks 2 Days 14 h 7 m 58 sec
Reputation Power: 5623
Facebook
Done in Access 2k3 it should work in VB2008 but may need something ... anyway the concept is solid

Code:
' // Will count unique letters in the string
' || Will *not* count numbers or other characters
Function UniqueLetters(strInput As String) As Integer
    Dim HasLetter(0 To 25) As Boolean ' A flag for each of the 26 letters of the alphabet
    Dim n As Integer                  ' A loop counter
    Dim c As Integer                  ' To get the letter
  ' // Loop through the string and flag each letter
    For n = 1 To Len(strInput)
      ' // Get the ASCII value then convert to 0(A)-25(Z)
      ' || Convert to upper case count A and a only as 1
        c = Asc(Mid(UCase(strInput), n, 1)) - 65
      ' // If it's a letter record that letter as counted
        If c >= 0 And c <= 25 Then
            HasLetter(c) = True
        End If
    Next n
  ' // Count the letters used and return result
    For n = 0 To 25
        If HasLetter(n) Then
            UniqueLetters = UniqueLetters + 1
        End If
    Next n
End Function

Sub Examples()
    Dim strPass As String
    strPass = "303 Lee Street" ' note it won't count the space or numbers
    MsgBox strPass & " has " & UniqueLetters(strPass) & " unique letters"
    strPass = "The Quick Brown Fox Jumped Over The Lazy Dogs"
    MsgBox strPass & " has " & UniqueLetters(strPass) & " unique letters"
    strPass = "equinox"
    MsgBox strPass & " has " & UniqueLetters(strPass) & " unique letters"
End Sub
__________________
medialint.com


"Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before. He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way."
- Vonnegut - Cat's Cradle, 1963

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Count common characters in two strings


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek