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?
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
Re: Need help on String manipulation...
Quote:
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.
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.
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.
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.
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
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.