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); }...
Python Programming Build a python programming that asks the user for a three-letter substring. The program...
Python Programming Build a python programming that asks the user for a three-letter substring. The program should then proceed to read the file strings.txt. Each line in strings.txt will contain a string. Your program should then test to see how many non-overlapping occurrences of the three-letter substring occur in that string and test whether the string is a palindrome. The program should then proceed to create an output file string_stats.txt, which contains the original data on one line and the...
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;...