Question

The function below returns a substring of input string. What is the invariant of this function?...

The function below returns a substring of input string.

What is the invariant of this function?

Static string substring( String in, int ind) {

String t = in ;

t = t.substring(ind);

return t;

}

Homework Answers

Answer #1

Answer:

Static string substring( String in, int ind) {
String t = in ;
t = t.substring(ind);
return t;
}

Here t.substring(ind) will return the string t from begin_index = ind to last_index = in.lenght() , in.length() will return the length of a string.
So here the invariant is the value of ind is greater than or eqaul to 0 and less than len(string).Because the String index starts from 0 and ends at length_of_string - 1.
So ind value must be between the above range other than that values it will raise an error(Index out of bound).

Invariant : 0 <= ind < in.lenght()

NOTE:
   If You Have Any Doubts Feel Free To Comment In The Comment Section.
   Do Vote (LIKE).

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function to check whether string s1 is a substring of string s2. The function...
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2) Write a program to test the function.
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Write a function name as 'square' that returns the square of input value. For example, the...
Write a function name as 'square' that returns the square of input value. For example, the main function is given: int main(){     cout << "The square of " << n << " is " << square(n) << endl;     return 0; } In c++ simple code.
Define a function called positivity generator. The function should take user input of a sentence, and...
Define a function called positivity generator. The function should take user input of a sentence, and find the first appearance of the substring 'not' and 'bad' from the user input. If 'not' appears before the 'bad', the function should replace the whole 'not'...'bad' substring with 'good'. Return the resulting string. Note your function should be able to handle user input with difference cases. Example: Input = 'The ice cream is not bad!' Output = 'The ice cream is good!" Input...
Fill in the code for the function below called containsAvgValue. It takes as input a float...
Fill in the code for the function below called containsAvgValue. It takes as input a float array of any length and returns 1 if the array happens to contain a value that equals the average of all the values in the array or 0 otherwise. For example, when give the array [2.0, 2.0] it should return 1 since the average value is 2.0 and the array contains that value. To help you, I’ve included a function that computes the average...
C++ Write a function that returns a string of 1's and 0's. This needs to be...
C++ Write a function that returns a string of 1's and 0's. This needs to be the binary representation of a number. Do not use loops. Use only recursion. Do not change the function's parameters or add more parameters. Do not change the main program. #include #include string bin(int number) { //Code here } int main() {    cout << bin(43) << endl; // Should be 101011    return 0; }
Complete the code below that aims to convert the String "input" to a static array of...
Complete the code below that aims to convert the String "input" to a static array of char named "array". You are NOT allowed to use String's toCharArray method! java String input = "Have you voted yet?"; char array[] =
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
Consider the recursive function pingPong described below. static String pingPong(int x) { if (x % 2...
Consider the recursive function pingPong described below. static String pingPong(int x) { if (x % 2 == 0 && x % 3 == 0) return "ping-pong"; else if (x % 2 == 0) return "ping," + pingPong(x - 1); else if (x % 3 == 0) return "pong," + pingPong(x - 1); else return "?," + pingPong(x - 1); } a) what's the base-case of pingPong? b) what's the result of pingPong(10)?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT