Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What arguments does each find function take? Are they changed? What special care

ID: 3841366 • Letter: W

Question

What arguments does each findfunction take? Are they changed? What special care should you take with them?

What value is returned by your functions? What type is it and what does it represent?

What care does a caller of your functions have to take with this return value? (i.e. Can they immediately assume it is a valid index?)

How does the compiler distinguish which of your functions is being used for a particular call? (They have the same name, after all...)

How do you protect your library from being circularly included?

What changes are needed in your main application (the test application here) to get it to work with the library? What about the compiling process?

Here is the program information:

Two useful functions that are not provided in the standard library are find a character in a string and find a substring in a string.

For instance, the character 'e'appears at position 2in the string "The quick brown fox". However, it does not appearin the string "cat's bat about yarn"at all. So our function could return a valid index if the character is found (2above) or -1(an invalid index) when it can't find the character.

Substrings are similar. The string "he"appears at position 1in "The quick brown fox". But, again, it does not appear at all in "cat's bat about yarn". And we can return -1or a valid index (such as 1here) to indicate where the substring was found.

Write these two functions — both named find. Place them in a library called strextra(since the name string is already taken).

Some further examples:

Other examples which can help to catch common coding errors:

Next write a test application for this library. A test application is basically a driver that tests all the functions in the library.

Explanation / Answer

public class HelloWorld{

  
// to find a character in a string
static int find(String a, char x)
{
int i=0, l = a.length();
  
// looping through each element and checking if equal
for(i=0;i<l;i++)
{
if(a.charAt(i)==x)
return i; // returns if matched
}
  
// if nothing matched, comes here and returns -1
return -1;
}
  
// to find a string in string
static int find(String a, String b)
{
int i=0,j, bl = b.length(), al = a.length();
  
// looping through a
for(i=0;i<=al-bl;i++)
{
// looping through b
for(j=0;j<bl;j++)
{
// checking if character in b matches that of a
if(b.charAt(j)!=a.charAt(i+j))
break;
}
  
// if successfully looped through all, then returning
if(j==bl)
return i;
}
  
// if nothing matched, comes here and returns -1
return -1;
}

public static void main(String []args){
// drivers
System.out.println(find("India","nd"));
System.out.println(find("India",'d'));
}
}

// Highlighted are the functions you requested

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote