• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyways thanks everyone for the help......
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[].
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic