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...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each. public class PurchaseDemo { public static void main(String[] args) { Purchase oneSale = new Purchase(); oneSale.readInput(); oneSale.writeOutput(); System.out.println("Cost each $" + oneSale.getUnitCost()); System.out.println("Total cost $" + oneSale.getTotalCost()); } } import java.util.Scanner; /**    Class for the purchase...
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.
Q3)   Write a C program that counts the number of odd numbers with using function count()...
Q3)   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...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT