Results 1 to 3 of 3
  1. #1
    Badprogrammer is offline Member
    Join Date
    Sep 2014
    Posts
    1
    Rep Power
    0

    Default replace chars without .replace

    Here is the task

    3. You are now going to write your own version of the string method .replace(x,y) called ForReplace.java. The user will type in a string, and then two characters and your program should go through the string character by character and replace each instance of the first letter with the second letter as it builds a new string. For instance, if the user types in:
    "Bandana man"
    n
    m
    Your program should output: Bamdama mam

    Notice how it only switches n into m and not vice versa.

    HINT: build up an separate String variable, character by character, like we did in class with the reversing example.

    NOTE: YOUR CODE SHOULD NOT USE THE BUILT IN .replace FUNCTION!!!! Submit ForReplace.java

    (fun fact: this is almost exactly how the actual String.replace(x,y) method is written)





    I started programming only a few weeks ago and im out of ideas. any tips?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: replace chars without .replace

    Quote Originally Posted by Badprogrammer View Post
    I started programming only a few weeks ago and im out of ideas. any tips?
    Post what you have done. Use the hints as provided in the writeup. If you are having
    problems with this then you should also discuss this with your instructor as your programming
    assignments will only get more difficult.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Hamenopi is offline Nub
    Join Date
    Sep 2014
    Location
    Houston
    Posts
    8
    Rep Power
    0

    Default Re: replace chars without .replace

    I hope this isn't a foul ball... but.

    Looks like the key to this problem is the two characters. You want one to become the other without using a .replace. You can do this (probably a million) different ways.
    characters all have a numerical value. For !!!FUN!!! You could:
    Java Code:
    get theDifference between character value a and character value b
    store theDifference as an integer
    for loop through each character in string
    if character value  equals character value a
    then add theDifference to character value

Similar Threads

  1. Dude in replace
    By spinter in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2012, 06:34 PM
  2. Find and replace
    By ravikanth.29 in forum AWT / Swing
    Replies: 1
    Last Post: 03-14-2011, 10:46 AM
  3. How to replace and integer with another
    By franzwarning in forum New To Java
    Replies: 7
    Last Post: 11-26-2010, 11:21 PM
  4. How to replace int with asterisk
    By erin.ctm in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:13 AM
  5. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •