Results 1 to 8 of 8

Thread: Need help on String manipulation...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    19

    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?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    19

    Re: Need help on String manipulation...

    Quote Originally Posted by LaVolpe View Post
    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    19

    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.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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
  •  



Click Here to Expand Forum to Full Width