Java > String-2 > starOut (CodingBat Solution)

Problem:

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"

Solution:

public String starOut(String str) {
int len = str.length();
String finalString = "";

for (int i = 0; i < len; i++) {
if (i == 0 && str.charAt(i) != '*')
finalString += str.charAt(i);

if (i > 0 && str.charAt(i) != '*' && str.charAt(i-1) != '*')
finalString += str.charAt(i);

if (i > 0 && str.charAt(i) == '*' && str.charAt(i-1) != '*')
finalString = finalString.substring(0,finalString.length()-1);




}
return finalString;
}


19 comments :

  1. My solution:
    public String starOut(String str) {
    String out = "";

    for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == '*' || (i > 0 && str.charAt(i-1) == '*')) continue;
    if (i < str.length()-1 && str.charAt(i+1) == '*') continue;

    out = out + str.charAt(i);
    }

    return out;
    }

    ReplyDelete
  2. i have been trying to use the String.replaceAll() method.
    however I cant figure out the regex to select all none 'word' chars.

    ReplyDelete
  3. try this one: str.replaceAll("(\\w?\\*\\w?)", "");

    ReplyDelete
    Replies
    1. oh my goodness you are a legend

      Delete
    2. where did you learn these type of solutions or are you creator of this site?

      Delete
    3. @anonymous from where you have learn these trick plz share to us, it will be very helpfull.

      Delete
  4. This is STAROUT solution into PLUSOUT topic.

    ReplyDelete
  5. public String starOut(String str) {
    StringBuilder sb = new StringBuilder("");
    for(int i = 0; i < str.length(); i++) {
    int left = (i >= 1) ? i - 1 : i;
    int right = (i < str.length() - 1) ? i + 1 : i;
    if(str.charAt(left) != '*' && str.charAt(right) != '*' && str.charAt(i) != '*') {
    sb.append(str.charAt(i));
    }
    }
    return sb.toString();
    }

    ReplyDelete
  6. return str.replaceAll("(.?[*]+.?)", "");

    ReplyDelete
  7. hi all,
    i would like to use this code and it works correctly but not appending the last character of str. could you advise?
    public String starOut(String str) {
    String result = "";
    int i=0;
    while (i<str.length()-1)
    {
    if (str.substring(i,i+1).equals("*"))
    {
    result = result;
    i=i+2;
    }
    else if (str.substring(i+1,i+2).equals("*"))
    {
    result = result;//.substring(0,i-1);
    i=i+3;
    }
    else
    {
    result = result+str.substring(i,i+1);
    i++;
    }
    }
    return result;
    }

    ReplyDelete
  8. This was my solution. It involves more lines of code, but has one fewer "if" statement within the for loop. So, perhaps it is more efficient?

    public String starOut(String str) {
    String result = "";

    //deal with empty strings;
    if(str.length() < 1){
    return result;
    }

    //advance starting index if str starts with '*'
    int start = 0;
    if(str.charAt(0) == '*'){
    start++;
    }

    for(int i = start; i < str.length(); i++){
    if(i > 0 && str.charAt(i-1) == '*'){
    continue;
    }
    result += str.charAt(i);
    if(str.charAt(i) == '*'){
    result = result.substring(0, result.length()-2);
    i++;
    }
    }

    return result;
    }

    ReplyDelete
  9. return str.replaceAll("[a-zA-Z1-9]?\\*+[a-zA-Z1-9]?", "");

    ReplyDelete
  10. public String starOut(String str) {
    String rep = "";
    for (int i = 0; i < str.length(); i++)
    {
    if (str.charAt(i) != '*')
    {
    if (i == 0)
    {
    if (i + 1 < str.length() && str.charAt(i + 1) != '*')
    {
    rep += str.charAt(i);
    }
    else if (str.length() == 1)
    {
    return str;
    }
    else
    {
    i += 2;
    }
    }
    else if (i == str.length() - 1)
    {
    if (i - 1 >= 0 && str.charAt(i - 1) != '*')
    {
    rep += str.charAt(i);
    }
    }
    else
    {
    if (str.charAt(i - 1) != '*' && str.charAt(i + 1) != '*')
    {
    rep += str.charAt(i);
    }
    }
    }
    }
    return rep;
    }

    ReplyDelete
  11. public String starOut(String str) {
    String star = "";
    for (int i = 0; i < str.length(); i++)
    {
    if (!(str.charAt(i) == '*' || (i + 1 < str.length() && str.charAt(i + 1) == '*') || (i - 1 >= 0 && str.charAt(i - 1) == '*')))
    {
    star += str.charAt(i);
    }
    //For faster implementation Below//
    else if (str.charAt(i) == '*')
    {
    i += 1;
    }
    else if (i + 1 < str.length() && str.charAt(i + 1) == '*')
    {
    i += 2;
    }
    }
    return star;
    }

    ReplyDelete
  12. public String starOut(String str) {
    if(str.length() >= 3){
    for(int i = 1; i < str.length()-1; i++){
    if(str.charAt(i) == '*' && str.charAt(i+1) != '*'){
    str = str.substring(0, i-1)+str.substring(i+2);
    if(str.length() == 0){
    return str;
    }
    i = i-1;
    }
    else if(str.charAt(i) == '*' && str.charAt(i+1) == '*'){
    str = str.substring(0, i-1)+str.substring(i);
    i = i-1;
    }
    }
    if(str.charAt(0) == '*'){
    str = str.substring(2);
    }
    if(str.charAt(str.length()-1) == '*'){
    str = str.substring(0, str.length()-2);
    }
    }
    if(str.length() == 1) {
    if(str.charAt(0) == '*'){
    str = "";
    }
    }
    else if(str.length() == 2 && str.charAt(0) == '*' ||
    str.length() == 2 && str.charAt(1) == '*'){
    str = "";
    }


    return str;
    }

    ReplyDelete
  13. return str.replaceAll("(.?[*]+.?)", "");

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact