• Post Reply Bookmark Topic Watch Topic
  • New Topic

indexOf implementation in Java  RSS feed

 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is for my wife who's a newbie in java. Can anyone please help? I am looking for the java method fully implemented..

"Problem:
Implement the strstr from string.h in the C standard library. This function is also know as index in Perl, and indexOf in Java and JavaScript.
The function should take two string parameters, short and long (note that these are just the variable names, not types), and return the first occurrence of short in long as a 0 based index into long. If short does not exist in long, the function should return -1.
you should provide a suite of test data along with your implementation.
Example: if str1=”cap”, str2=”escape”, applying strstr(str1,str2) should return 2

Constraints
You can use Java or C but MUST NOT use high level language facilities such as regular expressions or multi-character string comparisons.
If you choose to implement in C, you may return a char * that points to the character from long where the match begins, or NULL if short is not found.
This should be written as "production quality" code, meaning that it should be code you'd be willing to check in to a production tree. No extraneous debug statements, exit() calls, or anything else that would disturb production code.
The solution should be expressed in a single file with no outside dependencies. It should contain at least one function named strstr that performs the task, and one function named test that returns a true value if the strstr function passes all testcases. You may factor strstr into multiple functions if you like.
"
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch !
Please read this first.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The function should take two string parameters, short and long (note that these are just the variable names, not types),


It's not possible to implement this part of the requirement in Java
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne,
Why is that? I know out of the box indexOf() method does not have a (string,string) signature. Any specific reason why you think it can't be done?

Thanks
Sri
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sri Vatsa wrote:Hi Joanne,
Why is that? I know out of the box indexOf() method does not have a (string,string) signature. Any specific reason why you think it can't be done?

Thanks
Sri



"short" and "long" are reserved words in Java -- you can't have parameters with those names.

Henry
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh that, I think that's merely an example - probably intended to put off the programmer. Except that constraint, it can be done - right? I will try to do it tonight after work..
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, you do understand that you are not allowed to use the Java indexOf() method for this homework problem, right? If you do, you are violating this constraint.

You can use Java or C but MUST NOT use high level language facilities such as regular expressions or multi-character string comparisons.


The indexOf() method does a multi-character string comparision to find that index.

Henry
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand. Its my wife's homework - I am trying to help her out..
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
I think I am allowed to a character by character comparison in a nested for loop.

I don't think this qualifies a multi-character comparison, right. I am thinking of having 2 for loops and using break(); once a match is found (Note that I can't use any exit() calls).

Thanks
Sri
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good.

Henry
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not break() but break. Another reserved word.
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the basic body of StringTest working. Can you please help me modularize the program with test() functionality. I am referring to this particular requirement "The solution should be expressed in a single file with no outside dependencies. It should contain at least one function named strstr that performs the task, and one function named test that returns a true value if the strstr function passes all testcases."

I cannot use properties file for loading the test data since external dependecy is not allowed. Any hints?

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I cannot use properties file for loading the test data since external dependecy is not allowed. Any hints?


Sri,

Use java command line arguments to load data. If you dont need it anytime , you can check for null and assign your own value in the code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!