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,...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
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,...
In Java Part A Fred would like to sell his apartment. He would like to figure...
In Java Part A Fred would like to sell his apartment. He would like to figure out how much money he will spend at the closing. His lawyer needs to be paid $1,800 for his services. He needs to pay the co-op $500. Sellers pay a state and city combined transfer tax of 1.825 percent if the sale price is over $500,000 or 1.4 percent for deals under $500,000. The broker will collect 15% of the sale price at closing....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT