Results 1 to 6 of 6
  1. #1
    colin.cruise is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default Reverse and Replace a String in Linear Time

    It would be much appreciated if I could have some help with the following problem. I would like to write a Java function that has the following interface:
    public String reverseAndReplace(String original, String toBeReplaced, String replacement) { .. }

    This method would do two things:
    1) Reverse the words in the string.
    For example, given the string: "Wealth is the product of man's capacity to think." The method would transform this sentence to: "think. to capacity man's of product the is Wealth"
    2) Replace a word in the String with another word.
    For example:
    public String reverseAndReplace("Wealth is the product of man's capacity to think.", "think", "act");
    The method would return: "act. to capacity man's of product the is Wealth"

    This method should run in linear time, and it cannot use not any of Java's built-in string manipulation functions nor any third-party packages (such as Apache's StringUtils and the String.replaceAll methods) in its implementation. For example, this is illegal:

    public String reverseAndReplace(String original, String toBeReplaced, String replacement) {
    return StringUtils.reverseDelimited(original.replaceAll(t oBeReplaced, replacement), ' ');
    }

    Any help would be much appreciated and thanks in advance.

  2. #2
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Are you allowed to use split on a string?

  3. #3
    colin.cruise is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    No, String.split() is not allowed. But String.length,String.charAt(int index), String.toCharArray() is allowed.

  4. #4
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default Try this

    Java Code:
    public class StringReplace {
        
        public static void main(String[] args) {
            StringReplace replace = new StringReplace();
            String newSentence = replace.reverseAndReplace("backwards be should string This", "backwards", "forwards");
            System.out.println(newSentence);
        }
        
        public String reverseAndReplace(String original, String toBeReplaced, String replacement) {
            
            String[] backwardsSplit = original.split(" ");
            StringBuffer forwards = new StringBuffer();
            
            for(int i = backwardsSplit.length; i >0; i--) {
                String word = backwardsSplit[i - 1];
                if(word.equals(toBeReplaced)) {
                    forwards.append(replacement + " ");
                } else {
                    forwards.append(word + " ");
                }
            }
            
            return forwards.toString();
        }
        
    }

  5. #5
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    OK ignore that ^^ let me think of another way, I presume you are allowed to use equals?

  6. #6
    colin.cruise is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

Similar Threads

  1. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 02:21 AM
  2. Hash table with linear probing
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:43 PM
  3. Help me(Linear Multiobjective programming)
    By vinaytvijayan in forum Advanced Java
    Replies: 2
    Last Post: 01-22-2008, 08:24 AM
  4. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 06:51 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 09:14 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
  •