Results 1 to 4 of 4

Thread: Timed Out!?

  1. #1
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Timed Out!?

    this Is question: Given a string and a non-empty word string, return a version of the original String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.

    plusOut("12xy34", "xy") → "++xy++"
    plusOut("12xy34", "1") → "1+++++"
    plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"

    why my code give Timed Out?
    Java Code:
    	public String plusOut(String str, String word) {
    		int v=0;
    		String res = "";
    		for(int i=0;i<str.length();i++){
    			v = str.indexOf(word, i);
    			for(int z=i;z<v;z++){
    				res+="+";	
    			}
    			res += word;	
    			i += v-1;
    		}
    	return res;
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    What values does the i variable have as the loop is executed? And the v and z variables.
    Print them out to see.

  3. #3
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    finally i could solved it!
    Java Code:
    public String plusOut(String str, String word) {
    		int v=0;
    		String res = "";
    		for(int i=0;i<str.length();i++){
    			v = str.indexOf(word, i);
    			if(str.indexOf(word, i)<0){
    				for(int z=i;z<=str.length();z++){
    					res+="+";	
    				}
    				break;
    			}
    			for(int z=i;z<v;z++){
    				res+="+";	
    				}
    			res += word;	
    			i = v-1+word.length();
    		}
    	return res;
    	}

  4. #4
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What values does the i variable have as the loop is executed? And the v and z variables.
    Print them out to see.
    value of v was -1 and i got that it must be fixed ... thx.

Similar Threads

  1. Timed response button
    By fresh83 in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 08:01 PM
  2. javamail, Connection timed out
    By OmerHalit in forum Networking
    Replies: 11
    Last Post: 04-12-2010, 07:02 AM
  3. Connection Timed Out
    By sukatoa in forum Networking
    Replies: 2
    Last Post: 10-27-2009, 03:20 PM
  4. solution for connection timed out
    By santhosh_el in forum Networking
    Replies: 0
    Last Post: 10-03-2009, 07:55 AM
  5. i am getting connection timed out
    By santhosh_el in forum Networking
    Replies: 1
    Last Post: 08-25-2009, 06:57 AM

Posting Permissions

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