Java > String-1 > withoutX (CodingBat Solution)

Problem:

Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.

withoutX("xHix") → "Hi"
withoutX("xHi") → "Hi"
withoutX("Hxix") → "Hxi"


Solution:

public String withoutX(String str) {  
  if (str.length() == 0)
    return str;
  if (str.length() == 1){
    if (str.charAt(0) == 'x')
      return "";
    else
      return str;
  }
  
  if (str.charAt(0) == 'x')
    str = str.substring(1,str.length());
    
  if (str.charAt(str.length()-1) == 'x')
    str = str.substring(0,str.length()-1);

  return str;
}


13 comments :


  1. My fantasy world... f me.....


    public String withoutX(String str) {
    String sum="";

    if(!str.contains("x")){
    return str;
    }


    if(str.charAt(0)=='x'&&str.charAt(str.length()-1)!='x'){
    sum=sum+str.substring(1);
    }
    else if(str.charAt(0)!='x'&&str.charAt(str.length()-1)=='x'){

    sum=sum+str.substring(0,str.length()-1);
    }
    else if(str.charAt(0)=='x'&&str.charAt(str.length()-1)=='x'){
    for(int i=1;i<str.length()-1;i++){

    sum=sum+str.substring(i,i+1);
    }
    }
    else if(str.charAt(0)!='x'&&str.charAt(str.length()-1)!='x'){
    return str;

    }

    return sum;
    }


    ReplyDelete
  2. I find the easiest-to-understand solutions minimize the number of return statements, this solution only uses one:

    public String withoutX(String str) {
    StringBuilder result = new StringBuilder(str);

    if (!str.isEmpty() && str.length() > 1 && str.charAt(str.length()-1) == 'x') {
    result = result.deleteCharAt(str.length()-1);
    }

    if (!str.isEmpty() && str.charAt(0) == 'x') {
    result = result.deleteCharAt(0);
    }

    return result.toString();

    }

    ReplyDelete
  3. public String withoutX(String str) {

    if(str.length()==0 || str.length()==1)
    return "";
    else if(str.charAt(0) == 'x' && str.charAt(str.length()-1) == 'x')
    return str.substring(1,str.length()-1);
    else if(str.charAt(0) == 'x')
    return str.substring(1,str.length());
    else if(str.charAt(str.length()-1) == 'x')
    return str.substring(0,str.length()-1);

    else return str;
    }

    ReplyDelete
  4. public String withoutX(String str) {

    int len = str.length();
    String word = "";

    for (int i = 0; i < len; i++){
    if (i == 0 && str.charAt(0) != 'x') {
    word += str.charAt(0);
    } else if (i > 0 && i < len-1) {
    word += str.charAt(i);
    } else if (i == len-1 && str.charAt(len-1) != 'x') {
    word += str.charAt(len-1);
    }
    }
    return word;
    }

    ReplyDelete
  5. if (str.length() <= 1)
    return "";

    if (str.charAt(0) == 'x')
    str = str.substring(1, str.length());

    if (str.charAt(str.length()-1) == 'x')
    str = str.substring(0, str.length()-1);

    return str;

    ReplyDelete
  6. public String withoutX(String str) {
    if(str.startsWith("x")) str = str.substring(1);
    if(str.endsWith("x")) str = str.substring(0,str.length()-1);
    return str;
    }

    ReplyDelete
  7. if (str.length()>=1 && str.charAt(0) == 'x')
    str = str.substring(1,str.length());

    if (str.length()>=1 && str.charAt(str.length()-1) == 'x')
    str = str.substring(0,str.length()-1);

    return str;

    ReplyDelete
  8. public String withoutX(String str) {
    if(str.equals("x")){
    return "";
    }
    if(str.startsWith("x") && str.endsWith("x")){
    return str.substring(1, str.length() -1);
    }
    if(str.startsWith("x")){
    return str.substring(1);
    }
    if(str.endsWith("x")){
    return str.substring(0, str.length() - 1);
    }
    return str;
    }

    ReplyDelete
  9. public String repeatFront(String str, int n) {
    return (n == 0 || str.isEmpty())? "":
    str.substring(0, n) + repeatFront(str, n - 1);
    }

    ReplyDelete
  10. public String withoutX(String str) {
    if(str.length()<2){
    return "";
    }
    else if(str.substring(0,1).equals("x")&&str.substring(str.length()-1,str.length()).equals("x")){
    return str.substring(1,str.length()-1);
    }
    else if(str.substring(0,1).equals("x")){
    return str.substring(1);
    }
    else if (str.substring(str.length()-1,str.length()).equals("x")){
    return str.substring(0,str.length()-1);
    }
    return str;
    }

    ReplyDelete
  11. public String withoutX(String str) {
    if(str.length()>1 && str.startsWith("x") && str.endsWith("x"))return str.substring(1,str.length()-1);
    if(str.length()>1 && str.startsWith("H") && str.endsWith("x"))return str.substring(0,str.length()-1);
    if(str.length()>1 && str.startsWith("x") && str.endsWith("i"))return str.substring(1,str.length());
    else if(str.length()<2)return "";
    return str;
    }

    ReplyDelete
  12. public String withoutX(String str) {
    if (str.length()>=1 && str.charAt(0) == 'x')
    str = str.substring(1);

    if (str.length()>=1 && str.charAt(str.length()-1) == 'x')
    str = str.substring(0,str.length()-1);

    return str;

    }

    ReplyDelete
  13. In JavaScript:

    function withoutX(str){
    if(str.length == 1) {
    if(str[0] == 'x') {
    return '';
    } else {
    return str;
    }
    }
    if(str.startsWith('x') && str.endsWith('x')) {
    str = str.substring(1, str.length - 1)
    return str;
    } else if(str[str.length - 1] == 'x') {
    return str = str.substring(0, str.length - 1);
    } else if(str[0] == 'x') {
    return str = str.substring(1)
    } else {
    return str
    }
    }

    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