Question

Return a version of the given string, where for every star (*) in the string the...

Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".


starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly

Provided statement:

public String starOut(String str) {

//code goes here  

}

  

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

I hIS IS the m2.et hod บุ0u need.

public String starOut(String str) {
    String result = "";
    for(int i = 0; i < str.length(); ++i) {
        if(!(str.charAt(i) == '*' || (i != 0 && str.charAt(i-1) == '*') || (i != str.length()-1 && str.charAt(i+1) == '*'))) {
            result += str.charAt(i);
        }
    }
    return result;
}

\color{red}Below\;one\;is\;the\;complete\;program\;if\;you\;are\;interested..

public class StarOut {

    public String starOut(String str) {
        String result = "";
        for(int i = 0; i < str.length(); ++i) {
            if(!(str.charAt(i) == '*' || (i != 0 && str.charAt(i-1) == '*') || (i != str.length()-1 && str.charAt(i+1) == '*'))) {
                result += str.charAt(i);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        StarOut starOut = new StarOut();
        System.out.println(starOut.starOut("ab*cd"));
        System.out.println(starOut.starOut("ab**cd"));
        System.out.println(starOut.starOut("sm*eilly"));
    }

}

Add a comment
Know the answer?
Add Answer to:
Return a version of the given string, where for every star (*) in the string the...
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
  • This is in Python NOT JAVA. X516: starOut Feedback 0.0 / 1.0 Return a version of...

    This is in Python NOT JAVA. X516: starOut Feedback 0.0 / 1.0 Return a version of the given string, where for every star (*) in the string the star and the character immediately to its left and the character immediately to its right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad". Behavior Result Examples: starout("ab* cd") -> "ad" starout("ab**cd") -> "ad" 1 def starout(str):

  • Given a string, recursively compress all sets of repeating adjacent chars within an existing string to...

    Given a string, recursively compress all sets of repeating adjacent chars within an existing string to a single char. For example, "XVxzzz" yields "xyz" <pre> ceoveReaeatsl"fffaaac Quuutreturns "far Out" eoveReneatsC"nogoge wogorcriiies") returns "no worries" remaveReneats.C" Tomorrow") returns "Iomeo" s /pre Qparam stc a string of characters @return a version of the original string with all repeating adjacent sequences of the same character, reduced to a single character public static String removeRepeats (String str) { ou are forbidden to use any...

  • Java Programming Exercise Return true if the given string begins with "mix", except the 'm' can...

    Java Programming Exercise Return true if the given string begins with "mix", except the 'm' can be anything, so "pix", "9ix" .. all count. for example: mixStart("mix snacks") → true mixStart("pix snacks") → true mixStart("piz snacks") → false */ public boolean mixStart(String str) { *Need Help With Code Here* }

  • In Parts This Skill Builder will require you to write several functions in which loops will...

    In Parts This Skill Builder will require you to write several functions in which loops will be the focus. In addition, some of these function will require you to design and implement finite state machines. So, let's get started! The template below has a class called SkillBuilder6 with a set of skeleton methods provided. The requirements for each method is provided below Left Triangle In the template below, SkillBuilder6 has a method with the following signature: public static String leftRightTriangle...

  • C++ LANGUAGE /** * Given an input string, count the number of words ending * in...

    C++ LANGUAGE /** * Given an input string, count the number of words ending * in 'y' or 'z' -so the 'y' in "heavy" and the 'z' in "fez" * count, but not the 'y' in "yellow". Make sure that your * comparison is not case sensitive. We'll say that a y * or z is at the end of a word if there is not an alphabetic * letter immediately following it. * * Do not use any string...

  • This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string...

    This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string and a non-negative int n, return a larger string that is n copies of the original string. string_times('Hi', 2) – 'HiHi' string_times('Hi', 3) - 'HiHiHi' string_times('Hi', 1) – 'Hi' Solution: Go Save, Compile, Run (ctrl-enter) Show Solution def string_times (str, n): def string_times(str, n): result = "" for i in range(n): # range(n) is [0, 1, 2, .... n-1] result = result + str...

  • Teacher Instructions: Create the binarySearch() method for String arrays from the downloaded version for ints so...

    Teacher Instructions: Create the binarySearch() method for String arrays from the downloaded version for ints so the program can find String. Wrap a main around it, of course, so that we can see how it works. Provided Code: // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { int min =...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • I am trying to make a word guessing game on java, where there are four letters...

    I am trying to make a word guessing game on java, where there are four letters and you have 10 guesses to try and guess the letter combination. When I try to play, it says every combination of letters I enter is correct. For example: "You have 10 guesses left. Enter your guess: wxyz There are 4 correct letter placements. Congrats!" I emailed my professor and she said one of my methods is wrong because I'm only checking for specific...

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