Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2008, 09:25 PM
Member
 
Join Date: Jul 2008
Posts: 3
colin.cruise is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2008, 09:39 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
Are you allowed to use split on a string?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2008, 09:54 PM
Member
 
Join Date: Jul 2008
Posts: 3
colin.cruise is on a distinguished road
No, String.split() is not allowed. But String.length,String.charAt(int index), String.toCharArray() is allowed.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-01-2008, 09:58 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
Try this
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-01-2008, 09:59 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
OK ignore that ^^ let me think of another way, I presume you are allowed to use equals?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-01-2008, 10:02 PM
Member
 
Join Date: Jul 2008
Posts: 3
colin.cruise is on a distinguished road
Yes, String.equals() is allowed.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
string replace problem soni Advanced Java 8 07-06-2008 02:21 AM
Hash table with linear probing Java Tip java.lang 0 04-12-2008 09:43 PM
Help me(Linear Multiobjective programming) vinaytvijayan Advanced Java 2 01-22-2008 08:24 AM
Find and replace ( in a String hamish10101 New To Java 6 01-17-2008 06:51 AM
String replace method venkata.tarigopula Advanced Java 1 07-10-2007 09:14 PM


All times are GMT +3. The time now is 10:29 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org