Results 1 to 14 of 14

Thread: [2005] String manipulation (switching out vowels)

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    7

    [2005] String manipulation (switching out vowels)

    Hello! I've been trying for way too long to figure this out for how easy it should be...

    I'm trying to write a program for class, and while Im not looking to cheat, I just cant figure this out. I hope my code will show that Ive at least tried. This is not the only way I've tried to write this program, but I feel this code is my best effort.

    With that being said, what I need done is as simple as replacing vowels with Xs with an input no smaller than 5 characters and no larger than 7 within certain criteria, that of string manipulation (i.e. .Replace, .Contains) it seems like this should work, but it just isnt! Anyways, any input would be appreciated

    Code:
    Dim strOriginalPass As String
            Dim strNewPass As String
            Dim intCount As Integer
    
            strOriginalPass = txtOldPass.Text
    
            If strOriginalPass <= 5 > 7 Then
                Do While intCount < strOriginalPass.ToUpper
                    If strOriginalPass.ToUpper.Contains("[AEIOUY]") Then
                        strOriginalPass.Replace("[AEIOUY]", "X")
                    End If
    
                    intCount = intCount + 1
                Loop
                strNewPass = strOriginalPass
    
                lblOutput.Text = strNewPass
            Else
                MessageBox.Show("Input too short", "Test Solution")
            End If

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,187

    Re: [2005] String manipulation (switching out vowels)

    String.Replace is a function. Just like all string manipulations methods it does NOT make any changes to the current string. What it does is return a NEW String object with the specified replacements made.

    The MSDN documentation for the String.Replace method would have told you this and that's the first place you should have looked when it didn't work.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,187

    Re: [2005] String manipulation (switching out vowels)

    Note also that your use of Contains and Replace is wrong for another reason. You are testing specifically for the substring "[AEIOUY]", which your string is unlikely to contain. If you want to know whether your string contains any of those characters you would use IndexOfAny, and if you want to replace 6 characters then you need to call Replace 6 times: once for each character. The MSDN library is the place to look again.

  4. #4
    Frenzied Member Motoxpro's Avatar
    Join Date
    Sep 2001
    Location
    Spiro, OK
    Posts
    1,211

    Re: [2005] String manipulation (switching out vowels)

    This is what ive come up with, tried to keep your code the same. Probably an easier way to do this but im still learning also. Just add Y to the string list if you want to use that
    vb Code:
    1. Dim strOriginalPass As String
    2.         Dim strNewPass As String
    3.         Dim intCount As Integer
    4.         'Use a string list to define vowels
    5.         Dim Vowels As New List(Of String)
    6.         Vowels.Add("A")
    7.         Vowels.Add("E")
    8.         Vowels.Add("I")
    9.         Vowels.Add("O")
    10.         Vowels.Add("U")
    11.         'Convert the text in the textbox to uppercase so we dotn have to search twice
    12.         strOriginalPass = txtOldpass.Text.ToUpper
    13.         'Make sure the string is not to long or short
    14.         If strOriginalPass.Length >= 5 Or strOriginalPass.Length < 7 Then
    15.             'Use a loop to go through the string and check if any of our vowels are in the textbox
    16.             Do While intCount < Vowels.Count
    17.                 If strOriginalPass.Contains(Vowels(intCount)) Then
    18.                     strOriginalPass = strOriginalPass.Replace(Vowels(intCount), "X")
    19.                 End If
    20.                 'Go to the next vowel
    21.                 intCount = intCount + 1
    22.             Loop
    23.             strNewPass = strOriginalPass
    24.             lblOutput.Text = strNewPass
    25.         Else
    26.             MessageBox.Show("Input too short", "Test Solution")
    27.         End If
    Last edited by Motoxpro; Nov 19th, 2007 at 03:26 AM.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,187

    Re: [2005] String manipulation (switching out vowels)

    It's not a good idea to do other people's homework for them.

  6. #6
    Frenzied Member Motoxpro's Avatar
    Join Date
    Sep 2001
    Location
    Spiro, OK
    Posts
    1,211

    Re: [2005] String manipulation (switching out vowels)

    I'm sure he searched msdn but its hard to find what your looking for, all I did was rearange a few things he was on the right track

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    7

    Re: [2005] String manipulation (switching out vowels)

    Sigh, I shouldnt have mentioned the homework part, I'm not asking for somebody to do it for me.

    Anyways, jmcilhinney Thank you for your input, while it is probably apparent or maybe not apparent enough that I'm pretty new to .NET, I am just looking for genuine help. IndexOfAny sounds like the route I will take, assuming I can take it from there.

    Motoxpro, thanks for adding to my code, but I don't recognize the .Add method from any teachings I've had, but I do appreciate the input.

    Again, I wasn't looking for somebody to do my homework, which isn't even due til Tuesday and will be seeking assistance from my teacher when I see him tomorrow.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    7

    Re: [2005] String manipulation (switching out vowels)

    I agree with Motoxpro, MSDN isn't exactly the best resource as it can be difficult to decipher some of what they're saying. Although, I do use it.

  9. #9
    Addicted Member
    Join Date
    Sep 2007
    Location
    Right behind you
    Posts
    170

    Re: [2005] String manipulation (switching out vowels)

    im wondering why you need all that code, you just could have done 5 replaces, one after the other...

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,187

    Re: [2005] String manipulation (switching out vowels)

    You could also use a Regex and call Replace once only.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,187

    Re: [2005] String manipulation (switching out vowels)

    Quote Originally Posted by citan
    I agree with Motoxpro, MSDN isn't exactly the best resource as it can be difficult to decipher some of what they're saying. Although, I do use it.
    String.Replace:
    Quote Originally Posted by MSDN
    Replaces all occurrences of a specified String in this instance, with another specified String.
    Confusing?

  12. #12
    Addicted Member
    Join Date
    Sep 2007
    Location
    Right behind you
    Posts
    170

    Re: [2005] String manipulation (switching out vowels)

    ive never used regex, actually never heard of it before...

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,187

    Re: [2005] String manipulation (switching out vowels)

    Quote Originally Posted by winsrp
    ive never used regex, actually never heard of it before...
    The Regex class is the .NET implementation of regular expressions. It allows you to specify the pattern as any one of a number of characters and then Regex.Replace will replace each instance of any one of those characters with a different string.

  14. #14
    Addicted Member
    Join Date
    Sep 2007
    Location
    Right behind you
    Posts
    170

    Re: [2005] String manipulation (switching out vowels)

    hmmm.. sound interesting.... thanks for the info dude.

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