|
-
Sep 2nd, 2010, 08:16 AM
#1
Thread Starter
Junior Member
Need help on String manipulation...
For i = 1 To Len(Text1.Text)
For j = 1 To Len(Text1.Text)
If Not j = i Then
If Mid(Text1.Text, j, 1) = Mid(Text1.Text, i, 1) Then
Label1.Caption = Mid(Text1.Text, 1, j - 1) & Mid(Text1.Text, j + 1, Len(Text1.Text))
End If
End If
Next
Next
=======
this code is meant to display only the repeated letters for example if i entered the string "awa" the output would be "wa" but my problem here is that if i entered "aabbcc" the output is "aabbc"... how would i eliminate the other letters?
-
Sep 2nd, 2010, 09:01 AM
#2
Re: Need help on String manipulation...
Can you explain this better? Here are a few examples... what would the output look like for each one?
1. aabbcc
2. abcccccxyyyyz
3. abcabcabc
4. xyz123o321xyz
Last edited by LaVolpe; Sep 2nd, 2010 at 09:21 AM.
-
Sep 2nd, 2010, 09:35 AM
#3
Thread Starter
Junior Member
Re: Need help on String manipulation...
 Originally Posted by LaVolpe
Can you explain this better? Here are a few examples... what would the output look like for each one?
1. aabbcc
2. abcccccxyyyyz
3. abcabcabc
4. xyz123o321xyz
here are the answers...
1.abc
2.cy
3.abc
4.xyz123
it only display characters that has more than one occurence in the input.
-
Sep 2nd, 2010, 09:51 AM
#4
Thread Starter
Junior Member
Re: Need help on String manipulation...
sir LaVolpe, i am trying to make a program that would print whatever characters that are or has been repeated or has a more than one occurence on the given string.
-
Sep 2nd, 2010, 10:09 AM
#5
Re: Need help on String manipulation...
There are a few ways to handle that. Here are two.
1) When a duplicate is found do 2 things
a) add the character to a new output string
b) remove all occurrences of the character from the source string: Replace(char, "")
If going this route, realize your source string will change size each time duplicated character found
Edited: End result is source string contains only characters entered once & output string only characters entered more than once
2) Without modifying your source string, keep track of which duplicate was found
a) Look at your output string to see if character is already there & if so, skip the check
-- InStr(outputString, character)
b) If not in the output string, then check remaining source string to see if duplicated
c) When duplicate found, add character to output string
Edited: End result is source string unchanged & output string has only characters entered more than once. Faster, more efficient logic I'd think.
Last edited by LaVolpe; Sep 2nd, 2010 at 10:24 AM.
-
Sep 2nd, 2010, 11:23 AM
#6
Re: Need help on String manipulation...
Code:
Private Sub Command1_Click()
Dim Freq(255)
Text1.Text = "ababxxrrttjamalodh21209440,,&&@@=-" 'Set this to any string
Label1.Caption = "Repeated Characters: "
For I = 1 To Len(Text1.Text)
Freq(Asc(Mid$(Text1.Text, I, 1))) = Freq(Asc(Mid$(Text1.Text, I, 1))) + 1
Next
For I = 0 To 255
If Freq(I) > 1 Then Label1.Caption = Label1.Caption & Chr$(I)
Next
End Sub
This works with all characters. You can restrict the range as need be.
-
Sep 2nd, 2010, 11:50 AM
#7
Re: Need help on String manipulation...
Yep, definitely more than 1 way to skin that cat. Here's an example of option #2 in my previous reply
Code:
Dim i As Integer
Dim outString As String, sChar As String
For i = 1 To Len(Text1.Text) - 1
sChar = Mid$(Text1.Text, i, 1)
If InStr(outString, sChar) = 0 Then
If InStr(i + 1, Text1.Text, sChar) > 0 Then outString = outString & sChar
End If
Next
Label1.Caption = outString
Last edited by LaVolpe; Sep 2nd, 2010 at 11:55 AM.
Reason: tweaked for efficiency
-
Sep 2nd, 2010, 11:59 AM
#8
Re: Need help on String manipulation...
Good show, Fox. My code avoids the nested loop and Instr() but produces some redundancy. I'm not sure which would run faster for a huge string inside the text box. Redim Preserve could be used for the frequency count and then the last checking loop would only need to work with the bounds of the array. However, Redim Preserve tends to be lethargic.
My code could be surprisingly fast for huge input strings since only 256 If statements are ever required.
Last edited by Code Doc; Sep 2nd, 2010 at 04:55 PM.
Doctor Ed
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|