Question

Write an efficient method that takes a string txt and an integer M as arguments and returns the position of the first occurrence of M consecutive blanks in the string. If there is no such occurrence it should return the length of txt (txt.length). Give a runtime analysis of the method. 4.

In java please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Blank

{

public static void main(String args[])

{

String txt;//string

int M;//number of blanks.

Scanner s=new Scanner(System.in);

System.out.println("enter stirng and enter value for M");

txt=s.nextLine();

M=s.nextInt();

int l=txt.length(),i=0;//l is length of the string.i,j are for temporary use.

int j=0;

while (i<l)

{

if (txt.charAt(i)==' ')//if any blank is found ,checking for m consecutive blanks

{

j=i;

while (j<l)

{

if (txt.charAt(j)==' ')

{

j=j+1;

}

else

break;

}

if (j-i>=M)//if necessary number blanks are found

{

System.out.println("string found at"+i);

break;

}

i=j;

}

else

i=i+1;

}

if (i==l)//if string not found.

System.out.println("stirng not found and length="+l);

}

}

/*

Analysis of time complexity:

--------------------------------

-->charAt() method always takes constant time

-->The worst case number of iterations will be n

-->So, the time complexity will be =O(n).

-->you can verify with some test cases.*/

Add a comment
Know the answer?
Add Answer to:
In java please Write an efficient method that takes a string txt and an integer M...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • in java...also please give a little explanation thanks write a method that takes an integer and...

    in java...also please give a little explanation thanks write a method that takes an integer and returns its smallest factor greater than 1. Example: the smallest factor of 15 is 3, and the samllest factor of 25 is 5

  • in Java please (a) Write a static method abbreviate( ) which is passed a String s...

    in Java please (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .). For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too...

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • I need to write a method in java that returns void and takes a String element....

    I need to write a method in java that returns void and takes a String element. ex :public void addelemt(String element) the method job is to add elements to an ArrayList as many times as the person wants. please, no use of a switch or do.

  • Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n....

    Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n. The function computes the exclusive OR of the hashcodes (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--) of the first n strings in the array (result type int).

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted...

    Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted array a with n distinct integers, and an integer m. If there are two elements in the array that add to be m, the method returns True; if not, it returns False. You may assume that the elements in the array are all distinct. There are several ways to solve this problem! (and some solutions are worth more points than others!) For 6 points,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT