| Author |
How to find a particular substring in a string without using any builtin methods of string
|
amit kumar goyal
Greenhorn
Joined: Dec 07, 2006
Posts: 10
|
|
|
Hi my question is how can we find a particular string in another string without using any inbuilt String class methods. for eg. string "cofee" inside "please give me a cup of cofee". Thanks
|
 |
Vinoth Thirunavukarasu
Ranch Hand
Joined: Dec 18, 2008
Posts: 164
|
|
What do you mean
amit kumar goyal wrote:how can we find a particular string in another string without using any inbuilt String
|
Java Best Practices
Linux Best Practices
Amortization Calculator
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Hi Amit. Welcome to javaranch.
Well for that first you'll have to convert the string into a char array using, well a built in method of string class i.e. toCharArray and then implement a simple algorithm to search the char array. Or you can use the charAt method to check the characters at different indexes of the string and match it with corresponding characters of your other string. In both the cases you will have to use methods of the string class. I could have given you the exact code but here at ranch, we encourage people to try to solve simple problems like this on their own...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
amit kumar goyal
Greenhorn
Joined: Dec 07, 2006
Posts: 10
|
|
|
means does that string "cofee" or for that matter any string is there in another string.......i hope you got that...
|
 |
amit kumar goyal
Greenhorn
Joined: Dec 07, 2006
Posts: 10
|
|
|
Thanks for the reply....But i want a solution where we do not use any of the String class built in methods...
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
amit kumar goyal wrote:Thanks for the reply....But i want a solution where we do not use any of the String class built in methods...
Well, if you can't call *any* methods of the string class, then I believe it is *not* possible, as you won't be able to extract the data. Heck, to print the string to the screen, the internal libraries even use some of the methods.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
amit kumar goyal wrote:Thanks for the reply....But i want a solution where we do not use any of the String class built in methods...
why you want to do this? you want solution ? then how about this ?
|
 |
amit kumar goyal
Greenhorn
Joined: Dec 07, 2006
Posts: 10
|
|
|
I was asked this question in an interview.....Probably they want some solution like how it is implemented in the String class....the also asked a question like how can we find the length of a String without using any inbuilt methods......
|
 |
amit kumar goyal
Greenhorn
Joined: Dec 07, 2006
Posts: 10
|
|
|
Anyways thanks everyone for the help......
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
amit kumar goyal wrote:I was asked this question in an interview.....Probably they want some solution like how it is implemented in the String class....the also asked a question like how can we find the length of a String without using any inbuilt methods......
I think you are reading the question too strictly. I agree that they probably want some solution on how it is implemented -- which you should have answered... that is it held internally as a char array, and that you can get a copy of that array, using the toCharArray() method. I am sure that they don't mean that you can't call that method.
Then to answer the substring question, you need to talk about loops and checking characters.etc. For your second question, it is simply the length of the array.
Henry
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
As said before, you need at least toCharArray() or a combination of length() and charAt(int). Those are the only options to be able to inspect the contents of the strings.
Personally I would use length() and charAt(int) since it doesn't create a new char[].
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: How to find a particular substring in a string without using any builtin methods of string
|
|
|