Question

In: Computer Science

JAVA WITH NO IMPORTS Method changeWords executes a find and replace like you would with ctrl+f...

JAVA WITH NO IMPORTS

Method changeWords executes a find and replace like you would with ctrl+f
           or command+f on here
           The first value provided is the text that you search
           Turn the text into an array of strings
           find the string that is sent in the second position (find) and
           replace it with the

          changeWords("never have I ever", "ever", "cheated") returns ["never", "have", "I", "cheated"]
           changeWords("Why why Why", "Why", "because") returns ["becasue", "because", "because"]
           changeWords("What is up bro", "hi", "hello") returns ["what", "is", "up", "bro"]
           changeWords("Hi nice to meet you", "hi", "hello") returns ["hello", "nice", "to", "meet", "you"]
           changeWords( "", "", "") returns []

          @param String text, String of multiple words
           @param String find, the string to be replaced
           @param String replace, the string to be inserted in place of find
          @return String array, where words is the array of words after the edit is made
      */
public static String[] changeWords(String text, String find, String replace)
{
       //your code here
       String[] words = new String[0];//you are free to edit this line
return words;
}//end changeWords

Solutions

Expert Solution

import java.util.Arrays;

public class StringReplace {
        public static void main(String[] args) {
                String arr[]=changeWords("never have I ever", "ever", "cheated");
                String arr2[]=changeWords("Why why Why", "Why", "because");
                for(String x:arr)
                        System.out.print(x+" ");
                System.out.println();
                for(String x:arr2)
                        System.out.print(x+" ");
                
                
        }
        public static String[] changeWords(String text, String find, String replace)
        {
                // splitting the string into words
                String words[] = text.split(" ");
                // iterating through the words
                for(int i=0;i<words.length;i++) {
                        //checking if we have find than replcae it
                        if(words[i].equalsIgnoreCase(find))
                                words[i]=replace;
                }
                return words;
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot


Related Solutions

In Java For this assignment you will implement two methods, find() and replace() relating to Binary...
In Java For this assignment you will implement two methods, find() and replace() relating to Binary Search Trees. These methods are both to be recursive functions. The signatures for those functions must be: /* This method takes a tree node and a key as an argument and returns the tree node if the key is found and returns null if the key is not found. */ BST find(BST T, char key) /* This method takes a tree node and a...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
You will develop a program in C++ that will do a "find and replace" on an...
You will develop a program in C++ that will do a "find and replace" on an input file and write out the updated file with a new name. It will prompt your user for four inputs: The name of the input file. The characters to find in the input file. The replacement (substitute) characters. The name of the output file. It will perform the replacement. Note1: all instances must be replaced not just the first one on a line. Note2:...
You will develop a program in C++ that will do a "find and replace" on an...
You will develop a program in C++ that will do a "find and replace" on an input file and write out the updated file with a new name. It will prompt your user for four inputs: The name of the input file. The characters to find in the input file. The replacement (substitute) characters. The name of the output file. It will perform the replacement. Note1: all instances must be replaced not just the first one on a line. Note2:...
Part A) You have 67 grams of a unknown material, and you would like to find...
Part A) You have 67 grams of a unknown material, and you would like to find out its specific heat. You decide to heat it up to 100 C and put it in a 66 g aluminum cup (specific heat = 0.22 cal/(g C) that 200 grams of water (specific heat = 1 cal/(g C)). Usually this aluminum cup is in another cup so that the system is insulated. If both the cup and the aluminum cup are initially at...
You would like to purchase a home and are interested to find out how much you...
You would like to purchase a home and are interested to find out how much you can borrow. When your lender calculates your debt to income ratio, he determines that your maximum monthly payment can be no more than $4,349. You would like to have a 30 year fully- amortizing loan and the interest rate offered on such a loan is currently 3%. Given these constraints, what is the largest loan you can obtain? Round your answer to the nearest...
You would like to purchase a bond, you find one listed at 85.25, with an annual...
You would like to purchase a bond, you find one listed at 85.25, with an annual coupon rate of 6.5%, that matures in 8 years. Find the annual yield to maturity. If your required rate of return is 10% would you purchase this bond? Why or why not?
Java It would be nice if you use many Functions. rite a method, besides main(), which...
Java It would be nice if you use many Functions. rite a method, besides main(), which will calculate the lowest common multiple (LCM) of two numbers. For example, the multiples of 4 are 4, 8, 12, 16, 20, 24 …, and the multiples of 6 are 6, 12, 18, 24 …. The LCM is 12. Do NOT use any code from the Internet! Here are some more examples: 3:5 LCM is 15 4:8 LCM is 8 5:7 LCM is 35...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT