Skip to main content
deleted 22 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

HereThis is my codenot very memory-efficient. Can you please suggest a better way or improvement?

public class Test{

    public static void main(String[] args) {
        System.out.println(replaceSpace("All men must die"));
    }

    public static String replaceSpace(String s) {
        String[] stringArray = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(String s3 : stringArray) {
            sb.append(s3);
            sb.append("%20");
        }
        // if the last character is not space then, don't append %20.
        if(s.charAt(s.length()-1) != ' ') {
            return sb.substring(0, sb.length()-3).toString();
        }

        return sb.toString();
    }
}

It is not very memory efficient. Can you please suggest a better way or improvement.

Here is my code.

public class Test{

    public static void main(String[] args) {
        System.out.println(replaceSpace("All men must die"));
    }

    public static String replaceSpace(String s) {
        String[] stringArray = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(String s3 : stringArray) {
            sb.append(s3);
            sb.append("%20");
        }
        // if the last character is not space then, don't append %20.
        if(s.charAt(s.length()-1) != ' ') {
            return sb.substring(0, sb.length()-3).toString();
        }

        return sb.toString();
    }
}

It is not very memory efficient. Can you please suggest a better way or improvement.

This is not very memory-efficient. Can you please suggest a better way or improvement?

public class Test{

    public static void main(String[] args) {
        System.out.println(replaceSpace("All men must die"));
    }

    public static String replaceSpace(String s) {
        String[] stringArray = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(String s3 : stringArray) {
            sb.append(s3);
            sb.append("%20");
        }
        // if the last character is not space then, don't append %20.
        if(s.charAt(s.length()-1) != ' ') {
            return sb.substring(0, sb.length()-3).toString();
        }

        return sb.toString();
    }
}
Tweeted twitter.com/StackCodeReview/status/700549939936501760

Here is my code.

public class Test{

    public static void main(String[] args) {
        System.out.println(replaceSpace("All men must die"));
    }

    public static String replaceSpace(String s) {
        String[] stringArray = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(String s3 : stringArray) {
            sb.append(s3);
            sb.append("%20");
        }
        // if the last character is not space then, don't append %20.
        if(s.charAt(s.length()-1) != ' ') {
            return sb.substring(0, sb.length()-3).toString();
        } 

    
     return sb.toString();
    }
}

}

It is not very memory efficient. Can you please suggest a better way or improvement.

Here is my code.

public class Test{

public static void main(String[] args){
    System.out.println(replaceSpace("All men must die"));
}

public static String replaceSpace(String s){
    String[] stringArray = s.split(" ");
    StringBuffer sb = new StringBuffer();
    for(String s3 : stringArray) {
        sb.append(s3);
        sb.append("%20");
    }
    // if the last character is not space then, don't append %20.
    if(s.charAt(s.length()-1) != ' ') {
        return sb.substring(0, sb.length()-3).toString();
    }
    
     return sb.toString();
}

}

It is not very memory efficient. Can you please suggest a better way or improvement.

Here is my code.

public class Test{

    public static void main(String[] args) {
        System.out.println(replaceSpace("All men must die"));
    }

    public static String replaceSpace(String s) {
        String[] stringArray = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(String s3 : stringArray) {
            sb.append(s3);
            sb.append("%20");
        }
        // if the last character is not space then, don't append %20.
        if(s.charAt(s.length()-1) != ' ') {
            return sb.substring(0, sb.length()-3).toString();
        } 

        return sb.toString();
    }
}

It is not very memory efficient. Can you please suggest a better way or improvement.

Source Link
Mosbius8
  • 769
  • 2
  • 6
  • 13

Replace space with %20

Here is my code.

public class Test{

public static void main(String[] args){
    System.out.println(replaceSpace("All men must die"));
}

public static String replaceSpace(String s){
    String[] stringArray = s.split(" ");
    StringBuffer sb = new StringBuffer();
    for(String s3 : stringArray) {
        sb.append(s3);
        sb.append("%20");
    }
    // if the last character is not space then, don't append %20.
    if(s.charAt(s.length()-1) != ' ') {
        return sb.substring(0, sb.length()-3).toString();
    }
    
    return sb.toString();
}

}

It is not very memory efficient. Can you please suggest a better way or improvement.