File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find a particular substring in a string without using any builtin methods of string

 
amit kumar goyal
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
Android Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean
amit kumar goyal wrote:how can we find a particular string in another string without using any inbuilt String

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
amit kumar goyal
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyways thanks everyone for the help......
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[].
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic