• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

indexOf implementation in Java

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
"
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch !
Please read this first.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23956
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
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
Sounds good.

Henry
 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not break() but break. Another reserved word.
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic