Results 1 to 6 of 6
- 07-01-2008, 08:25 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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.
- 07-01-2008, 08:39 PM #2
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
Are you allowed to use split on a string?
- 07-01-2008, 08:54 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
No, String.split() is not allowed. But String.length,String.charAt(int index), String.toCharArray() is allowed.
- 07-01-2008, 08:58 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
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(); } }
- 07-01-2008, 08:59 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
OK ignore that ^^ let me think of another way, I presume you are allowed to use equals?
- 07-01-2008, 09:02 PM #6
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
string replace problem
By soni in forum Advanced JavaReplies: 8Last Post: 07-06-2008, 01:21 AM -
Hash table with linear probing
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:43 PM -
Help me(Linear Multiobjective programming)
By vinaytvijayan in forum Advanced JavaReplies: 2Last Post: 01-22-2008, 07:24 AM -
Find and replace ( in a String
By hamish10101 in forum New To JavaReplies: 6Last Post: 01-17-2008, 05:51 AM -
String replace method
By venkata.tarigopula in forum Advanced JavaReplies: 1Last Post: 07-10-2007, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks