Question

In: Computer Science

Write a method in java (with out using startwith,boolean true false) count that counts how many...

Write a method in java (with out using startwith,boolean true false) count that counts how many times a substring occurs in a string:For example: count("is", "Mississippi") will return 2. also add comment besides code.

Solutions

Expert Solution

//code screenshot

//code to copy

public class Count {

static int count(String substr,String mainstr)

{

int subLen = substr.length();

int mainLen= mainstr.length();

int count = 0;

  

/* A loop to slide substr one by one */

for (int i = 0; i <= mainLen - subLen; i++) {

/* For current index i, check for

pattern match */

int j;

for (j = 0; j < subLen; j++) {

if (mainstr.charAt(i + j) != substr.charAt(j)) {

break;

}

}

  

//if pattern substr match than inrease count by 1

if (j == subLen) {

count++;

j = 0;

}

}

return count; //return how many time string is repeted

}

public static void main(String[] args)

{

int countval=count("is", "Mississippi");

System.out.println("Count of substring in main String:"+countval);

}

}

//output screenshot


Related Solutions

Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
Determine the value, true or false, of each of the following Boolean expressions, assuming that the...
Determine the value, true or false, of each of the following Boolean expressions, assuming that the value of the variable count is 0 and the value of the variable limit is 10. Give your answer as one of the values true or false. a. (count == 0) && (limit < 20) b. count == 0 && limit < 20 c. (limit > 20) || (count < 5) d. !(count == 12) e. (count == 1) && (x < y) f....
Using the Lumen Method to find out how many fittings are required for a square conference...
Using the Lumen Method to find out how many fittings are required for a square conference room which requires 500 lux from CIBSE Guide, of area 200 m2, using the T5 fluorescent fittings at 1.2 meter long each and at 2 m high measured from the light source to the working surface. The 1.2m T5 luminaire has 2700 lumens each. Your answer should mention about all the light losses, say, due to surface reflectance at ceiling, wall and floor, light...
Write TRUE if the statement is TRUE. If the statement is FALSE, write FALSE and explain...
Write TRUE if the statement is TRUE. If the statement is FALSE, write FALSE and explain why 3. The Commissioner of Internal Revenue may prescribe presumptive gross sales and receipts for a taxpayer when the latter fails to issue receipts and when he believes that the books or other records of the latter do not correctly reflect declarations in the return. 4. Jurisdiction is the power and authority of the court to hear, try, and decide a case. It can...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Consider the following definitions given to you for Boolean logic: not = λx.((x false) true) true...
Consider the following definitions given to you for Boolean logic: not = λx.((x false) true) true = λx.λy.x false = λx.λy.y Show that not (not true) evaluates to true. Your steps must use proper substitutions and each step must be accompanied with a brief explanation (e.g., "replacing the bound x with false", etc.). You can use "L" for the lambda symbol in your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT