• Post Reply Bookmark Topic Watch Topic
  • New Topic

HOW to replace space instead of %20 in web url  RSS feed

 
P Arunkumar
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


I have atten interview they told to write code for this question.

Question:

There is a web URL which contains %20 in place of space, how will you replace every %20 with space? Write the program / function in java.
( Do not use inbuilt fun/ regular expression - write your own algorithm).


Example :

String URL= "http://abcat/abcclient/mypages%20all%20produts/forms%20dir/AllItems.aspx;"

It wants to show like below

String URL= "http://abcat/abcclient/mypages all produts/forms dir/AllItems.aspx;"


I Know only regular expression i check in google It is telling "You have to write your own algorithm, you can use KMP to answer this question"

I can't understand what is KMP.

My regular expression is




Pleaase guide me to write code in own algorithm.


Thanks in advance.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cannot use any built-in search / replace function, you'll have to do it "by hand": you'd create a new String (preferably using a StringBuilder) and traverse the original String character by character. While copying, you'd look for the "%20" sequence and if you encounter it, you'd add a space character instead.

The only complication is that, as per the requirement, you need to copy other sequences starting with % into the output string without transformation. However, it is not that hard to do it by hand. Try it yourself, and if you get stuck, come back here for help.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Arunkumar wrote:Pleaase guide me to write code in own algorithm.

I suspect they wanted to know whether you knew about String.replace() or String.replaceAll().
Either one will do the trick, although the first is probably quicker (and all you need in your case).

Winston

[Edit] Ooops. Didn't read the question properly. Forget what I said and follow Martin's advice.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohana, please note that the Ranch is not a code mill. Instead of handing the users "the code", we prefer to help them find the solution themselves, as it is always the best way to learn. See also DontBeACodeMill and LetThemDoTheirOwnHomework (<-- click) for additional points.

I'll leave your solution here, as it is a good step in the right direction, but it does not fully implement the requirements. However, let the original user tackle the remaining details on his own, please.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have attended interview they told to write code for this question.


Martin it's not homework question. He was struggling to doing it. I thought to write a wrong logic and ask him to correct. But I feel that might mislead him. I'm aware of code of conduct in JavaRanch. Hope you understand me.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mind answering tricky and misleading interview questions outright, but this one was neither especially tricky, nor misleading. I sincerely believe the original poster would greatly benefit from trying to solve this one on his own before his next interview. This is why I gave very rough pointers and was ready to answer follow-up questions.

Mohana Rao Sv wrote:... I thought to write a wrong logic and ask him to correct. ...

That's an interesting idea, though I'd say it would be better if you told so in your previous post. Actually, I have not removed your code exactly because it still needs some polishing from the original poster. As I wasn't sure about your motives, I've replied the way I did. I'd say it's all clear now.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed addicted to use built-in functionality @Arun it's your turn to code clean-up.
 
Umang Bhatt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String URL = "http://abcat/abcclient/mypages all produts/forms dir/AllItems.aspx";
URL = URL.replaceAll(" ", "%20");
System.out.println(URL);
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umang Bhatt wrote:String URL = "http://abcat/abcclient/mypages all produts/forms dir/AllItems.aspx";
URL = URL.replaceAll(" ", "%20");
System.out.println(URL);

Umang, please read Martin's second post.

We do not like people to simply hand out solutions - particularly as, based on OP's original post, yours is wrong (in fact it's just plain wrong regardless).

Winston
 
P Arunkumar
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


Thanks a lot,

Really your code is so help full for me, from your code i got some knowlwdge and how i wants to think different also i have learned, in my office no one is not there to traine me , so to think different it is difficult. If some one help me like this really i will improve me.


He gave me a code, but by using this only i got some knowledge, like me it will be help full for some one. Really with my full heart iam telling thanks very much.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Official Oracle tutorials are an excellent learning resource. If you're relatively new to Java, definitely read all of them in the Trails Covering the Basics section (you might skip Swing if you're not interested in desktop - or "thick client" - applications). In my opinion, they are even better than many books on the market. Though books, especially in your first language, can be helpful too.

And even if you cannot ask questions at your workplace, we'll be glad if you ask questions here. This is what this forum is for in the first place. We just don't want to hand out "the code" -- when in difficulties, show us what have you tried and where you got stuck, and we'll try to help you as much as we can.

Good luck with Java!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!